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)
评论列表
文章目录