Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the issue of Chinese character garbled encoding in CSV export. #1288

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ihatemoney/tests/import_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def test_export(self):
'2016-12-31,red wine,XXX,200.0,jeanne,1.0,"zorglub, tata"',
'2016-12-31,fromage à raclette,10.0,XXX,zorglub,2.0,"zorglub, jeanne, tata, pépé"',
]
received_lines = resp.data.decode("utf-8").split("\n")
received_lines = resp.data.decode("utf-8-sig").split("\n")

for i, line in enumerate(expected):
assert set(line.split(",")) == set(received_lines[i].strip("\r").split(","))
Expand Down Expand Up @@ -457,7 +457,7 @@ def test_export(self):
"55.34,XXX,jeanne,tata",
"127.33,XXX,jeanne,zorglub",
]
received_lines = resp.data.decode("utf-8").split("\n")
received_lines = resp.data.decode("utf-8-sig").split("\n")

for i, line in enumerate(expected):
assert set(line.split(",")) == set(received_lines[i].strip("\r").split(","))
Expand Down Expand Up @@ -656,7 +656,7 @@ def test_export_escape_formulae(self):
"date,what,amount,currency,payer_name,payer_weight,owers",
"2016-12-31,'=COS(36),10.0,EUR,zorglub,1.0,zorglub",
]
received_lines = resp.data.decode("utf-8").split("\n")
received_lines = resp.data.decode("utf-8-sig").split("\n")

for i, line in enumerate(expected):
assert set(line.split(",")) == set(received_lines[i].strip("\r").split(","))
Expand Down
4 changes: 2 additions & 2 deletions ihatemoney/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ def list_of_dicts2csv(dict_to_convert):
writer = csv.writer(csv_file)
writer.writerows(csv_data)
csv_file.seek(0)
csv_file = BytesIO(csv_file.getvalue().encode("utf-8"))
csv_file = BytesIO(csv_file.getvalue().encode("utf-8-sig"))
return csv_file


def csv2list_of_dicts(csv_to_convert):
"""Take a csv in-memory file and turns it into
a list of dictionnaries
"""
csv_file = TextIOWrapper(csv_to_convert, encoding="utf-8")
csv_file = TextIOWrapper(csv_to_convert, encoding="utf-8-sig")
reader = csv.DictReader(csv_file)
result = []
for r in reader:
Expand Down
Loading