Xuan Xuan - 3 years ago 225
JSON Question

Unicode encoding issue with Parsing JSON to CSV using csvwriter


with open(read_json,'r',encoding='utf-8') as json_file:

json_data = json.load(json_file)
with open(write_csv,'w',encoding='utf-8') as csv_file:

headers, items = parse_json(json_data,query_type)
# i is to be iterated to get all maxResults = 50.
writer = csv.writer(csv_file)
for row in items:

CSV file:

enter image description here
enter image description here

Im having weird characters in my CSV files not exactly sure whats happening.

Answer Source

Windows applications often assume text files are encoded in an ANSI encoding, which varies by localized Windows version. Windows-1252 is the encoding used by U.S. Windows.

Excel also makes this assumption, but if it sees a UTF8 BOM signature, it will use UTF8 for the encoding. Use the file encoding utf-8-sig to write this signature. If used to open a file for reading, it recognizes and discards the signature so it doesn't affect your Python code.

So use utf-8-sig instead to give Excel the hint it needs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download