def index_file(self, filename):
"""Reads a datafile and returns a dict index
of the starting lines for each year and lists of starting lines for each
month over each year.
"""
import calendar
_reads = 0
_return_dict = {}
with open(filename, 'r') as _data_file:
_last_year = ''
_last_month = ''
for _data_line in _data_file:
_reads += 1
try:
_year = str(_data_line[13:17])
_month = calendar.month_abbr[int(_data_line[17:19])]
if _year != _last_year:
_return_dict[_year] = _reads - 1
_last_year = _year
if _month != _last_month:
try:
_return_dict[_month].append(_reads - 1)
except KeyError:
_return_dict[_month] = [_reads - 1]
_last_month = _month
except ValueError:
continue
return _return_dict
评论列表
文章目录