I’m encountering an issue when writing data to a CSV file in Python. Some characters are appearing as question marks inside a square box, which I believe indicates an encoding problem.
When I open the CSV file in Notepad, the characters display correctly (for example, “ö” is shown as expected). However, when I open the same file in Excel, some characters appear as question marks inside a square box.
`
def write_word_counts_to_csv(self,word_counts, list_of_all_words:list, output_file):
try:
with open(output_file, ‘w’, newline=”, encoding=”utf_8″) as csvfile:
fieldnames = [‘PII’] + list_of_all_words
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for doc_count in word_counts:
pii = list(doc_count.keys())[0]
counts = doc_count[pii]
row = {'PII': pii}
row.update(counts) #This adds all the key-value pairs from the counts dictionary to the row dictionary
writer.writerow(row)
`
What can I do to ensure that all characters are correctly encoded and displayed in both Notepad and Excel?