When writing to csv, the writerow file does not work with UnicodeEncodeError

I have a line:

c.writerow(new_values) 

This writes a series of values ​​to the csv file. It usually works fine, but sometimes it throws an exception and does not write the line to 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 on my own exception, and he 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> 
+6
source share
2 answers

Ok, I solved it myself:

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

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

The csv module in python is known for not handling Unicode characters very well. If all characters do not fall into ascii code, you probably won't be able to write a string. There is a (somewhat) fallback replacement called unicodecsv that you might want to explore. https://pypi.python.org/pypi/unicodecsv

0
source

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


All Articles