Step_1.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:Wind-Turbine-Design 作者: doublerob7 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号