when writing to csv file writerow fails with UnicodeEncodeError

前端 未结 2 448
南方客
南方客 2021-01-17 12:48

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 a

相关标签:
2条回答
  • 2021-01-17 13:00

    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

    0 讨论(0)
  • 2021-01-17 13:05

    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'))
    
    0 讨论(0)
提交回复
热议问题