# Reading data files with headers and columns

**URL:** https://rubytalk.org/t/reading-data-files-with-headers-and-columns/52272
**Category:** ruby-talk
**Created:** [13 March 2009 17:07 UTC](https://rubytalk.org/t/reading-data-files-with-headers-and-columns/52272 "2009-03-13T17:07:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Yaj\_Bhattacharya](https://avatars.discourse-cdn.com/v4/letter/y/9de053/32.png) [@Yaj\_Bhattacharya](https://rubytalk.org/u/Yaj_Bhattacharya)
#### Post date: [13 March 2009 17:07 UTC](https://rubytalk.org/t/reading-data-files-with-headers-and-columns/52272/1 "2009-03-13T17:07:51Z")

</div>

Hello,

As a noob to Ruby, I need to read a data file with the first few lines  
as header, then the rest of the file as columns of different variables  
as arrays (in this case, 5 columns).  
After reading in the variables, I want to calculate some operations  
with each unique data field address (e.g. column3, row4), then write  
out the file with headers and data in columns.

Could someone help with a few lines of basic code?

Thanks in advance  
Yaj

---

<div class="post-metadata">

### Author: ![Luc](https://avatars.discourse-cdn.com/v4/letter/l/9dc877/32.png) [@Luc](https://rubytalk.org/u/Luc)
#### Post date: [13 March 2009 17:23 UTC](https://rubytalk.org/t/reading-data-files-with-headers-and-columns/52272/2 "2009-03-13T17:23:48Z")

</div>

Yaj Bhattacharya a écrit :

> Hello,
> 
> As a noob to Ruby, I need to read a data file with the first few lines  
> as header, then the rest of the file as columns of different variables  
> as arrays (in this case, 5 columns).  
> After reading in the variables, I want to calculate some operations  
> with each unique data field address (e.g. column3, row4), then write  
> out the file with headers and data in columns.
> 
> Could someone help with a few lines of basic code?
> 
> Thanks in advance  
> Yaj

You could start with this basic piece of code (replace \<tags\> with your code):  
&nbsp;&nbsp;# here's the columns as arrays  
&nbsp;&nbsp;@column1 =   
&nbsp;&nbsp;\<rest\_of\_columns\>  
&nbsp;&nbsp;# open file in read mode (r)  
&nbsp;&nbsp;file = File.new("\<your\_filename", "r")  
&nbsp;&nbsp;# read each line one after another and process it  
&nbsp;&nbsp;file.each\_line do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;# process as header if necessary  
&nbsp;&nbsp;&nbsp;&nbsp;# or  
&nbsp;&nbsp;&nbsp;&nbsp;# extract your variables in the line into an array  
&nbsp;&nbsp;&nbsp;&nbsp;array = line.split("\<your\_column\_separator\>")  
&nbsp;&nbsp;&nbsp;&nbsp;# assign variable to column  
&nbsp;&nbsp;&nbsp;&nbsp;@column1 \<\< array[0]  
&nbsp;&nbsp;&nbsp;&nbsp;# ... process other columns  
&nbsp;&nbsp;end

Additionnally you can google "ruby array" and "ruby file", the API docs are well documented.

Luc

---

<div class="post-metadata">

### Author: ![Yaj\_Bhattacharya](https://avatars.discourse-cdn.com/v4/letter/y/9de053/32.png) [@Yaj\_Bhattacharya](https://rubytalk.org/u/Yaj_Bhattacharya)
#### Post date: [13 March 2009 18:02 UTC](https://rubytalk.org/t/reading-data-files-with-headers-and-columns/52272/3 "2009-03-13T18:02:51Z")

</div>

Thanks very much Luc, here are a few follow up questions (complete  
noob)

# here's the columns as arrays  
&nbsp;&nbsp;@column1 = # should this be @column1=[variable1] or just the  
empty square brackets?  
&nbsp;&nbsp;\<rest\_of\_columns\>  
&nbsp;&nbsp;# open file in read mode (r)  
&nbsp;&nbsp;file = File.new("\<your\_filename", "r")  
&nbsp;&nbsp;# read each line one after another and process it  
&nbsp;&nbsp;file.each\_line do |line| # where do I specify how the header is to  
be skipped, how many lines to  
#be skipped or otherwise processed?  
&nbsp;&nbsp;&nbsp;&nbsp;# process as header if necessary  
&nbsp;&nbsp;&nbsp;&nbsp;# or  
&nbsp;&nbsp;&nbsp;&nbsp;# extract your variables in the line into an array  
&nbsp;&nbsp;&nbsp;&nbsp;array = line.split("\<your\_column\_separator\>") # I am guessing that  
a space column separator  
#would be " ", does it work with arbit number of multiple spaces  
between the columns i.e. if the  
#column width is variable but between two  
# columns there are one or several spaces?  
&nbsp;&nbsp;&nbsp;&nbsp;# assign variable to column  
&nbsp;&nbsp;&nbsp;&nbsp;@column1 \<\< array[0]  
# if @column1=[variable1] comment in the second line of the above code  
was not correct, then  
#where do the variables get their names?  
&nbsp;&nbsp;&nbsp;&nbsp;# ... process other columns  
&nbsp;&nbsp;end

> **···**
>
> On Mar 13, 1:23 pm, Luc Traonmilin \<luc.traonmi...@gmail.com\> wrote:
> 
> > Yaj Bhattacharya a écrit :
> > 
> > \> Hello,
> > 
> > \> As a noob to Ruby, I need to read a data file with the first few lines  
> > \> as header, then the rest of the file as columns of different variables  
> > \> as arrays (in this case, 5 columns).  
> > \> After reading in the variables, I want to calculate some operations  
> > \> with each unique data field address (e.g. column3, row4), then write  
> > \> out the file with headers and data in columns.
> > 
> > \> Could someone help with a few lines of basic code?
> > 
> > \> Thanks in advance  
> > \> Yaj
> > 
> > You could start with this basic piece of code (replace \<tags\> with your  
> > code):  
> > # here's the columns as arrays  
> > @column1 =   
> > \<rest\_of\_columns\>  
> > # open file in read mode (r)  
> > file = File.new("\<your\_filename", "r")  
> > # read each line one after another and process it  
> > file.each\_line do |line|  
> > # process as header if necessary  
> > # or  
> > # extract your variables in the line into an array  
> > array = line.split("\<your\_column\_separator\>")  
> > # assign variable to column  
> > @column1 \<\< array[0]  
> > # ... process other columns  
> > end
> > 
> > Additionnally you can google "ruby array" and "ruby file", the API docs  
> > are well documented.
> > 
> > Luc- Hide quoted text -
> > 
> > - Show quoted text -

---

<div class="post-metadata">

### Author: ![Luc](https://avatars.discourse-cdn.com/v4/letter/l/9dc877/32.png) [@Luc](https://rubytalk.org/u/Luc)
#### Post date: [13 March 2009 18:30 UTC](https://rubytalk.org/t/reading-data-files-with-headers-and-columns/52272/4 "2009-03-13T18:30:17Z")

</div>

Well only initializes the array as empty, if you want to initialize it  
with variables it is [var1, var2...]. Alternatively, you can use a Hash:  
@column1 = {"var1" =\> value, ...}  
and then access the values with @column1["var1"] if that is what you want to  
do.

I don't know what your header looks like so it is difficult to say how to  
skip it. Maybe it is possible for you to match the line against the pattern  
of your columns if you just want to skip the header. Or if you have a fixed  
number of lines, you can use a counter.

> **···**
>
> On Fri, Mar 13, 2009 at 7:02 PM, Yaj Bhattacharya \<yajnaval@gmail.com\>wrote:
> 
> > Thanks very much Luc, here are a few follow up questions (complete  
> > noob)
> > 
> > # here's the columns as arrays  
> > &nbsp;&nbsp;@column1 = # should this be @column1=[variable1] or just the  
> > empty square brackets?  
> > &nbsp;&nbsp;\<rest\_of\_columns\>  
> > # open file in read mode (r)  
> > file = File.new("\<your\_filename", "r")  
> > # read each line one after another and process it  
> > &nbsp;&nbsp;file.each\_line do |line| # where do I specify how the header is to  
> > be skipped, how many lines to  
> > #be skipped or otherwise processed?  
> > &nbsp;&nbsp;&nbsp;&nbsp;# process as header if necessary  
> > &nbsp;&nbsp;&nbsp;# or  
> > &nbsp;&nbsp;&nbsp;# extract your variables in the line into an array  
> > &nbsp;&nbsp;&nbsp;&nbsp;array = line.split("\<your\_column\_separator\>") # I am guessing that  
> > a space column separator  
> > #would be " ", does it work with arbit number of multiple spaces  
> > between the columns i.e. if the  
> > #column width is variable but between two  
> > # columns there are one or several spaces?  
> > &nbsp;&nbsp;&nbsp;&nbsp;# assign variable to column  
> > &nbsp;&nbsp;&nbsp;@column1 \<\< array[0]  
> > # if @column1=[variable1] comment in the second line of the above code  
> > was not correct, then  
> > #where do the variables get their names?  
> > &nbsp;&nbsp;&nbsp;# ... process other columns  
> > end
> > 
> > On Mar 13, 1:23 pm, Luc Traonmilin \<luc.traonmi...@gmail.com\> wrote:  
> > \> Yaj Bhattacharya a écrit :  
> > \>  
> > \>  
> > \>  
> > \> \> Hello,  
> > \>  
> > \> \> As a noob to Ruby, I need to read a data file with the first few lines  
> > \> \> as header, then the rest of the file as columns of different variables  
> > \> \> as arrays (in this case, 5 columns).  
> > \> \> After reading in the variables, I want to calculate some operations  
> > \> \> with each unique data field address (e.g. column3, row4), then write  
> > \> \> out the file with headers and data in columns.  
> > \>  
> > \> \> Could someone help with a few lines of basic code?  
> > \>  
> > \> \> Thanks in advance  
> > \> \> Yaj  
> > \>  
> > \> You could start with this basic piece of code (replace \<tags\> with your  
> > \> code):  
> > \> # here's the columns as arrays  
> > \> @column1 =   
> > \> \<rest\_of\_columns\>  
> > \> # open file in read mode (r)  
> > \> file = File.new("\<your\_filename", "r")  
> > \> # read each line one after another and process it  
> > \> file.each\_line do |line|  
> > \> # process as header if necessary  
> > \> # or  
> > \> # extract your variables in the line into an array  
> > \> array = line.split("\<your\_column\_separator\>")  
> > \> # assign variable to column  
> > \> @column1 \<\< array[0]  
> > \> # ... process other columns  
> > \> end  
> > \>  
> > \> Additionnally you can google "ruby array" and "ruby file", the API docs  
> > \> are well documented.  
> > \>  
> > \> Luc- Hide quoted text -  
> > \>  
> > \> - Show quoted text -
> 
> --  
> Luc Traonmilin

---

<div class="post-metadata">

### Author: ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)
#### Post date: [13 March 2009 19:19 UTC](https://rubytalk.org/t/reading-data-files-with-headers-and-columns/52272/5 "2009-03-13T19:19:42Z")

</div>

If you know how many header lines, I'll assume 3, just read them first before you start the loop:

headers =   
rows =   
File.open("your\_filename", 'r') do |file|  
&nbsp;&nbsp;&nbsp;3.times { headers \<\< file.gets }

&nbsp;&nbsp;&nbsp;file.each do |line|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rows \<\< line.split(' ') # or ',' or /,\s\*/ depending  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# on how "columns" are formed  
&nbsp;&nbsp;&nbsp;end  
end # the block given to File.open will automatically close the file

# for the value in 3rd column, 4th row (counting from ZERO)

rows[3][2]

# do your calculations, then open the same or a new file for writing:

File.open("your\_output", 'w') do |file|  
&nbsp;&nbsp;&nbsp;file.puts headers  
&nbsp;&nbsp;&nbsp;rows.each do |row|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file.puts row.join(' ') # or however you separate columns  
&nbsp;&nbsp;&nbsp;end  
end

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Mar 13, 2009, at 2:30 PM, Luc Traonmilin wrote:
> 
> > Well only initializes the array as empty, if you want to initialize it  
> > with variables it is [var1, var2...]. Alternatively, you can use a Hash:  
> > @column1 = {"var1" =\> value, ...}  
> > and then access the values with @column1["var1"] if that is what you want to  
> > do.
> > 
> > I don't know what your header looks like so it is difficult to say how to  
> > skip it. Maybe it is possible for you to match the line against the pattern  
> > of your columns if you just want to skip the header. Or if you have a fixed  
> > number of lines, you can use a counter.
> > 
> > On Fri, Mar 13, 2009 at 7:02 PM, Yaj Bhattacharya \> \<yajnaval@gmail.com\>wrote:
> > 
> > > Thanks very much Luc, here are a few follow up questions (complete  
> > > noob)
> > > 
> > > # here's the columns as arrays  
> > > @column1 = # should this be @column1=[variable1] or just the  
> > > empty square brackets?  
> > > \<rest\_of\_columns\>  
> > > # open file in read mode (r)  
> > > file = File.new("\<your\_filename", "r")  
> > > # read each line one after another and process it  
> > > file.each\_line do |line| # where do I specify how the header is to  
> > > be skipped, how many lines to  
> > > #be skipped or otherwise processed?  
> > > &nbsp;&nbsp;&nbsp;# process as header if necessary  
> > > &nbsp;&nbsp;# or  
> > > &nbsp;&nbsp;# extract your variables in the line into an array  
> > > &nbsp;&nbsp;&nbsp;array = line.split("\<your\_column\_separator\>") # I am guessing that  
> > > a space column separator  
> > > #would be " ", does it work with arbit number of multiple spaces  
> > > between the columns i.e. if the  
> > > #column width is variable but between two  
> > > # columns there are one or several spaces?  
> > > &nbsp;&nbsp;&nbsp;# assign variable to column  
> > > &nbsp;&nbsp;@column1 \<\< array[0]  
> > > # if @column1=[variable1] comment in the second line of the above code  
> > > was not correct, then  
> > > #where do the variables get their names?  
> > > &nbsp;&nbsp;# ... process other columns  
> > > end
> > > 
> > > On Mar 13, 1:23 pm, Luc Traonmilin \<luc.traonmi...@gmail.com\> wrote:
> > > 
> > > > Yaj Bhattacharya a écrit :
> > > > 
> > > > > Hello,
> > > > 
> > > > > As a noob to Ruby, I need to read a data file with the first few lines  
> > > > > as header, then the rest of the file as columns of different variables  
> > > > > as arrays (in this case, 5 columns).  
> > > > > After reading in the variables, I want to calculate some operations  
> > > > > with each unique data field address (e.g. column3, row4), then write  
> > > > > out the file with headers and data in columns.
> > > > 
> > > > > Could someone help with a few lines of basic code?
> > > > 
> > > > > Thanks in advance  
> > > > > Yaj
> > > > 
> > > > You could start with this basic piece of code (replace \<tags\> with your  
> > > > code):  
> > > > # here's the columns as arrays  
> > > > @column1 =   
> > > > \<rest\_of\_columns\>  
> > > > # open file in read mode (r)  
> > > > file = File.new("\<your\_filename", "r")  
> > > > # read each line one after another and process it  
> > > > file.each\_line do |line|  
> > > > &nbsp;&nbsp;&nbsp;# process as header if necessary  
> > > > &nbsp;&nbsp;&nbsp;# or  
> > > > &nbsp;&nbsp;&nbsp;# extract your variables in the line into an array  
> > > > &nbsp;&nbsp;&nbsp;array = line.split("\<your\_column\_separator\>")  
> > > > &nbsp;&nbsp;&nbsp;# assign variable to column  
> > > > &nbsp;&nbsp;&nbsp;@column1 \<\< array[0]  
> > > > &nbsp;&nbsp;&nbsp;# ... process other columns  
> > > > end
> > > > 
> > > > Additionnally you can google "ruby array" and "ruby file", the API docs  
> > > > are well documented.
> > > > 
> > > > Luc- Hide quoted text -
> > > > 
> > > > - Show quoted text -
> > 
> > --  
> > Luc Traonmilin

---

<div class="post-metadata">

### Author: ![Yaj\_Bhattacharya](https://avatars.discourse-cdn.com/v4/letter/y/9de053/32.png) [@Yaj\_Bhattacharya](https://rubytalk.org/u/Yaj_Bhattacharya)
#### Post date: [17 March 2009 12:42 UTC](https://rubytalk.org/t/reading-data-files-with-headers-and-columns/52272/6 "2009-03-17T12:42:34Z")

</div>

Thanks Rob!  
Thanks Luc!
