flux2stock.py 文件源码

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

项目:sirene 作者: etalab 项目源码 文件源码
def write_stock(stock_out, filtered_stock, modifications):
    """
    Generate the new stock file with modified and created entries.

    We mimick the initial stock with encoding, quotes and delimiters.
    """
    with open(stock_out, 'w', encoding='cp1252') as csv_file:
        _, first_row, fieldnames = next(filtered_stock)
        # `extrasaction` is set to `ignore` to be able to pass more keys
        # to the `writerow` method coming from the flux.
        writer = csv.DictWriter(
            csv_file, fieldnames=fieldnames, delimiter=';',
            quoting=csv.QUOTE_ALL, extrasaction='ignore')
        writer.writeheader()
        # Because we already iterate once to retrieve fieldnames.
        writer.writerow(first_row)
        # Then write the updated stock.
        for i, row, _ in filtered_stock:
            writer.writerow(row)
        # Finally, append creations and insertions.
        for siret, row in modifications.items():
            is_created = row['VMAJ'] == 'C'
            is_inserted = row['VMAJ'] == 'D'
            if is_created or is_inserted:
                writer.writerow(row)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号