Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

problem in encoding .csv file content conversion to .json file

I use this piece of code to convert a .csv file content to a .json file:

import csv
import json

with open('export.csv', encoding='utf-8') as f:
    reader = csv.DictReader(f)
    rows = list(reader)

with open('test.json', 'w', encoding='utf-8') as f:
    json.dump(rows, f)

The conversion is successful, but Arabic characters in .csv file convert to something like this in .json file:

"\u06a9\u0627"

How can I solve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

According to json.dump docstring:

If ensure_ascii is false, then the strings written to fp can
contain non-ASCII characters if they appear in strings contained in
obj. Otherwise, all such characters are escaped in JSON strings.

Solution:

json.dump(rows, f, ensure_ascii=False)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading