when writing to csv file writerow fails with UnicodeEncodeError

泪湿孤枕 提交于 2019-12-08 15:33:09

问题


I have the line:

c.writerow(new_values)

That writes a number of values to a csv file. Normally it is working fine but sometimes it throws an exception and doesn't write the line in the csv file. I have no idea how I can find out why.

This is my exception handling right now:

        try:
            c.writerow(new_values)
        except:
            print()
            print ("Write Error: ", new_values)

I commented out my own exception and it says:

    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u03b1' in position 14: character maps to <undefined>

回答1:


Ok, I solved it by myself:

I just had to add ", encoding='utf-8'" to my csv.writer line:

c = csv.writer(open("Myfile.csv", 'w',  newline='', encoding='utf-8'))



回答2:


the csv module in python is notorious for not handling unicode characters well. Unless all characters fall in the ascii codec you probably won't be able to write the row. There is a (somewhat) drop in replacement called unicodecsv that you may want to look into. https://pypi.python.org/pypi/unicodecsv



来源:https://stackoverflow.com/questions/29773991/when-writing-to-csv-file-writerow-fails-with-unicodeencodeerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!