Skip to content
This repository was archived by the owner on May 2, 2026. It is now read-only.

Commit 4de4445

Browse files
Rafał Miłeckirmilecki
authored andcommitted
output: to_csv: print header for every set of parsed fields
When parsing multiple invoices it's *incorrect* to: 1. Display header with fields names of the *first* parsed invoice 2. Display content of *all* parsed invoices It's because parsing dfferent invoices may result in different fields. In such case header won't make sense for all rows. To improve handling such situations display new header for every set of fields that differs from the last one. Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
1 parent bc667cf commit 4de4445

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

src/invoice2data/output/to_csv.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ def write_to_file(data: list, path: str, date_format="%Y-%m-%d") -> None:
4141
with openfile as csv_file:
4242
writer = csv.writer(csv_file, delimiter=",")
4343

44+
last_header = None
4445
for line in data:
4546
first_row = []
4647
for k, v in line.items():
4748
first_row.append(k)
4849

49-
writer.writerow(first_row)
50-
for line in data:
50+
if first_row != last_header:
51+
writer.writerow(first_row)
52+
last_header = first_row
53+
5154
csv_items = []
5255
for k, v in line.items():
5356
# first_row.append(k)

0 commit comments

Comments
 (0)