simonw / csv-diff

Python CLI tool and library for diffing CSV and JSON files
Apache License 2.0
292 stars 47 forks source link

Support for empty CSV files #18

Open gfiorav opened 3 years ago

gfiorav commented 3 years ago

Thanks for this lib!

I've noticed that the compare method doesn't work if one of the csv files is empty.

Steps to reproduce

a.csv

id,foo,bar

b.csv

id,foo,bar
1,hey,ho
2,lets,go
from csv_diff import compare
from csv_diff import load_csv

diff: dict = compare(
    load_csv(open("a.csv"), key="id"),
    load_csv(open("b.csv"), key="id"),
)

for added_row in added:
     print(added_row)

Expected result

{"id": 1, "foo": "hey", "bar": "ho"}
{"id": 2, "foo": "lets", "bar": "go"}

Actual result

Traceback (most recent call last):
  File "REDACTED.py", line 165, in <module>
    diff: dict = compare(
  File "REDACTED/python3.9/site-packages/
csv_diff/__init__.py", line 58, in compare
    previous_columns = set(next(iter(previous.values())).keys())
StopIteration

I'm using Python 3.9.2.

sid1333 commented 2 years ago

File "REDACTED/python3.9/site-packages/ csvdiff/__init_\.py", line 58, in compare

previous_columns = set(next(iter(previous.values())).keys()) if previous else set()
current_columns = set(next(iter(current.values())).keys()) if current else set()