Reputation: 867
I am writing a large list that includes some non-ascii characters into a csv file using the csv
module. I keep getting the following error message
UnicodeEncodeError: 'ascii' codec can't encode characters in position 48-50: ordinal not in range(128)
I don't need those characters so it's ok to ignore them. I tried different ways recommended on SOF but none of them worked so far (the error message is the same). Is there an easy way to get around the encoding error and proceed? I tried:
io.open(file, encoding = "utf-8")
codecs.open(file, encoding = "utf-8")
codes.open(file, errors = "ignore")
I can write the data into csv using the pandas
module by simply setting encoding = "utf-8"
. Can I do something similar in csv
module?
Upvotes: 2
Views: 1958
Reputation: 5275
You can install unicodecsv or you can write your row in utf-8 format while writing in your CSV
file using csv writer
.
import csv
with open("sample.csv", "w") as wf:
writer = csv.writer(wf)
writer.writerow([unicode(s, "utf-8") for s in row])
Upvotes: 0
Reputation: 122536
For Python 2.7 you should use the unicodecsv
module: unicodecsv 0.9.4. This is a replacement for Python's built-in csv
module and it does support Unicode. In Python 3.x you no longer need this replacement as Python 3.x does support Unicode in its csv
module.
You can install it by running:
pip install unicodecsv
Upvotes: 4