Writing unicode data to csv

I know that a similar question has been asked many times, but seriously, I could not correctly implement csv-writer, which writes correctly to csv (it shows garbage).

I am trying to use UnicodeWriter as a reference in white papers .

ff = open('a.csv', 'w') writer = UnicodeWriter(ff) st = unicode('Displaygrößen', 'utf-8') #gives (u'Displaygr\xf6\xdfen', 'utf-8') writer.writerow([st]) 

This does not give me any decoding or coding error. But he writes the word Displaygrößen as Displaygrößen , which is not very good. Can someone help me what I'm doing wrong here?

+4
source share
2 answers

You write the file in UTF-8 format, but do not specify it in your csv file.

You must write the UTF-8 header at the beginning of the file. Add this:

 ff = open('a.csv', 'w') ff.write(codecs.BOM_UTF8) 

And your csv file should be opened correctly after that, when the program tries to read it.

+6
source

Opening the file with codecs.open should fix it.

0
source

Source: https://habr.com/ru/post/1411102/


All Articles