utils.py 文件源码

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

项目:us_zipcodes_congress 作者: OpenSourceActivismTech 项目源码 文件源码
def load_csv_columns(filename, column_names=None, skip=0, delimiter=',', quoting=csv.QUOTE_MINIMAL):
    r = []
    log.info('opening', filename)
    with open(filename, 'r') as f:
        data_file = csv_reader_converter(f, delimiter=delimiter, quoting=quoting)
        for i in range(skip):
            next(data_file)
        headers = next(data_file, None)  # parse the headers
        columns = {}
        for (i, h) in enumerate(headers):
            h = h.strip()
            if (not column_names) or h in column_names:
                columns[i] = h
        log.info("headers", headers)
        log.info("columns", column_names)

        for line in data_file:
            d = {}
            if not line:
                continue
            for (column, index) in columns.items():
                if column_names:
                    rename = column_names[index]
                else:
                    rename = headers[column]
                value = line[column].strip()
                d[rename] = value
            r.append(d)
        log.info('read %d lines' % len(r))
        return r
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号