Below is a sample of my dictionary:
dict = {'Croatia': '191', 'Cuba': '192', 'Curaçao': '531',
'Cyprus': '196', 'Czechia': '203', 'Czechoslovakia': '200',
"Côte d'Ivoire": '384', "Dem. People's Rep. of Korea": '408',
'Dem. Rep. of the Congo': '180', 'Denmark': '208'}
My goal is to try to write the diction into a csv file so each row will have one key and one value, such as:
Croatia, 191
Cuba, 192
and I am using csv for this purpose:
import csv
with open('dict.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
for key, value in dict.items():
writer.writerow([key.encode('utf-8'), value])
when not using key.encode('utf-8'), Python gives the error of 'ascii' codec can't encode character '\xf4' in position 1: ordinal not in range(128), presumably cuased by Côte d'Ivoire in the dictionary. However, even when a csv file can be successfully produced now, the csv file itself contains additional characters b'countryname' instead of countryname.
(see the image for reference: https://i.sstatic.net/F9dJu.jpg)
How do I solve this particular issue?