I am getting encoding issue and ensure_ascii=False issue while writing to a csv file

107 views Asked by At

I have list of dictionaries and I loaded those dictionaries from json files. I stored those json files with my code as following

import json
data = {Some Json}
with open('test.json','w',encoding='ISO-8859-1') as file:
   json.dump(data,file,indent=4,ensure_ascii=False)

When I use ensure_ascii it stores correctly. I have German characters which only can be stored using ISO-8859-1. The utf-8 or anything is not working correctly. Here are some characters

Größe
ä

But the issue came when I tried to store the list of dictionaries into a csv file this does not work. When I try to save a csv file from it as I cannot ensure_ascii=False there as it only uses with json.dump() so I do not get characters correctly in csv. What I got in csv was replacement character �� Here is what i tried to store csv file

csv_file = "data.csv"
with open(csv_file, mode="w", newline="",encoding='ISO-8859-1',errors='replace') as file:
     writer = csv.DictWriter(file, fieldnames=mainList[0].keys())
     writer.writeheader()
     for row in mainList:
         writer.writerow(row)

So I should get words Größe but i get Gr��e in csv file. I tried searching on google,etc but didn't get any solution.

If you can help I appreciate it. Thank you in advance.

1

There are 1 answers

0
JimChr - R4GN4R On

This script saves the json special characters fine at my side:

import json

jsonfile = {"Größe": "ä"}

with open('test.json','w',encoding='utf8') as file:
   json.dump(jsonfile,file,indent=4,ensure_ascii=False)