0
votes

I am trying to wrap my head around this for a while now and have not yet seen any solution that does not confuse me.

I got a script in python that should write an array with words (German Names) into an excel file.

cell = [name_1, name_2, name_3]   

import csv
 fl = open('company_data.csv', 'w')

 writer = csv.writer(fl)
 writer.writerow(['Name_1', 'Name_2', 'Name_3']) 
 for values in cell:
     writer.writerow(values)

 fl.close() 

The error that comes is ...,line 135, in writer.writerow(values) UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 0: ordinal not in range(128) [Finished in 1.2s with exit code 1]

The names include the German characters ü,ä,ö etc.

How do I fix this?

1
It's nearly a year, but have you tried using python3 explicitly instead of python only? Had the same problem, had a #!/usr/bin/env python shebang, with #!/usr/bin/env python3 everything worked fine. If you're not on *nix, use python3 myfile.py and not python myfile.py :) - criztovyl

1 Answers

0
votes

I think you have to open the file and specify that you want to write unicode. Aussming you want utf-8:

import codecs
fl = codecs.open("company_data.csv", "w", "utf-8")