Re: Counting header lines in a file [message #94606 is a reply to message #94605] |
Sat, 22 July 2017 03:26   |
Nikola
Messages: 53 Registered: November 2009
|
Member |
|
|
On Friday, July 21, 2017 at 9:59:09 PM UTC+1, thtr...@gmail.com wrote:
> Hello guys,
>
> I have a .dat file that looks like this:
>
> Date: May 5, 2016
> Name: a person's name goes here
> Experiment with temperature blabla
>
> Day. Temperature
> 1 56
> 2 62
> 3 63
> 4 95
>
> ___________________________________________
> Anyway, you get the idea.
> So I'm trying to read in just the numeric part of the data (the 2 cols of numbers), and ignore the headers.
> Here's what I have so far:
>
> lineofheaders = 5
> file = 'tryhard.dat'
> rows = file_lines(file) ;count rows of entire file
> openr, lun, file, /get_pun
> header = strarr(lineofheaders). ;pre-allocate to read the header
> readf, lun, header
> point_lun, -lun, currentlocation
> line =""
> readf, lun, line
> cols = n_elements(float(strsplit(line, /regex)))
> data = fltarr(cols,rows - lineofheaders)
> point_lun, lun, currentlocation
> readf, lun, data
> free_lun, lun
>
> The above code did work, of course. However, my problem is that I always have to know in advance the number of lines that the header takes up. For example, in this file the header takes up 5 lines, so I will start reading data from line 6 till end of file.
>
> Is there any way to still do the above, without knowing in advance how many lines the header takes up?
>
> Thank you so much.
> Thomas
Once you read a line, you can parse it using the string functions. For example, strmid(line, 0, 1) returns the first character of the string. Then you can test if it is a letter or a number so that you can decide if it belongs to the header or to the data.
|
|
|