我是一名有用的助手,可以为您翻译文本。
例如,文件中的第一条记录如下所示:
我将以上内容称为一条记录(一个月的所有数据),文件中大约有1200条记录。月份从1到12依次递增,然后以年份的增量重新开始。我想逐条读取记录,类似于以下方式:
什么是一种高效的“Pythonic”方法来循环遍历记录,包括如何最好地将数据读入列表或数组?
我有一个ASCII数据文件,其格式对于我来说很陌生,不知道如何最好地将数据读入Python中的列表或数组。 ASCII数据文件的格式如下:
line 0: <month> <year>
lines 1 - 217: 12 integer values per line, each value has seven spaces, the first is always a space
例如,文件中的第一条记录如下所示:
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.>
我将以上内容称为一条记录(一个月的所有数据),文件中大约有1200条记录。月份从1到12依次递增,然后以年份的增量重新开始。我想逐条读取记录,类似于以下方式:
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
什么是一种高效的“Pythonic”方法来循环遍历记录,包括如何最好地将数据读入列表或数组?