Im trying to write a response which is in json format to a csv file using csvwriter. below is the code:
import csv
import requests
import codecs
url = "xxxx"
data = requests.get(url).json()
with codecs.open("xxx.csv", 'w', encoding='utf-8') as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=['id', 'name', 'age', 'company', 'sex', 'job', 'time', 'main', 'sub', 'thor'])
writer.writeheader()
for row in data:
writer.writerow(row)
This is the error i keep getting
Traceback (most recent call last):
File "so_test.py", line 15, in <module>
writer.writerow(row)
File "/usr/lib64/python2.7/csv.py", line 148, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 8: ordinal not in range(128)
tried using io.open , open(file, 'wb', 'utf-8'). nothing worked. Can someone help?