I have an ASCII data file with a format that's unfamiliar to me in terms of how I could best read the data into a list or array in Python. The ASCII data file is formatted like this:
line 0: <month> <year>
lines 1 - 217: 12 integer values per line, each value has seven spaces, the first is always a space
For example the first record in the file looks like this:
1 1900
-32768 -32768 790 -1457 -1367 -16 -575 116 -32768 -32768 1898 -32768
-32768 -1289 -32768 -32768 -32768 -32768 -32768 -32768 -32768 -32768 -32768 -32768
-32768 -32768 -92 -32768 -32768 -32768 125 -32768 -32768 -32768 -32768 -32768
-32768 -32768 -32768 -32768 -32768 -1656 -32768 -764 -32768 -32768 -32768 -32768
<212 more lines like the above for this record, same spacing/separators/etc.>
I'll call the above a single record (all data for a single month), and there are about 1200 records in the file. The months increase sequentially from 1 to 12 before starting over with an increment of the year value. I want to read the records one at a time, something like this:
with open(data_file, 'r') as dataFile:
# while file still has unread records
# read month and year to use to create a datetime object
# read the next 216 lines of 12 values into a list (or array) of 2592 values
# process the record's list (or array) of data
Can someone suggest an efficient "Pythonic" way of doing the above looping over the records including how to best read the data into a list or array?
Thanks in advance for your help!