我写了这个fallowing方法来解析这个文件,但是无法读取最近一年(2014年)的不完整数据,该数据在文本文件表中是空白的。
现在我正在跳过我读不懂的句子。在
帮我解决这个问题吗?。在LINES_TO_IGNORE = 7
import collections
import csv
def parse_file(data_file):
result_dict = collections.OrderedDict()
if not data_file:
return result_dict
with open(data_file) as f:
reader = csv.reader(f, delimiter="\t")
data = islice(reader, LINES_TO_IGNORE, None, None)
if not data:
return result_dict
# Get file headers
headers = data.next()
headers = headers[0].split()
keys = headers[1:]
for row in data:
values = row[0].split()
if len(headers) == len(values):
year = parse_to_int(values[0])
data_list = [parse_to_float(x) for x in values[1:]]
# Each line becomes a dict (column_header->value)
data_dict = collections.OrderedDict(zip(keys, data_list))
else:
print "Skipping"
# result_dict is dict of dict (year->data_dict)
result_dict[year] = data_dict
return result_dict