1

How can I get csv.writer.writerow to output á or è and other special characters along the same lines u, n....etc?

I am getting an error UnicodeEncodeError: 'ascii' codec can't encode character u'\xed' in position 37: ordinal not in range(128)

Have tried some of the suggestions in this article with no avail

rowPrinter = []

while x < y:
     print "Data in at Line " + str(x + 1)
     rowPrinter.append([a[x], b[x], c[x]]])
     x = x + 1   

x = 0

writer = csv.writer(outcsv, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
writer.writerow(['a', 'b', 'c'])
while x < y:
    print rowPrinter[x]
    writer.writerow(rowPrinter[x])
0

1 Answer 1

0

You need to encode all the data that you are trying to write:

writer.writerow([sub.encode("utf-8") for sub in rowPrinter[x]])

If you have a mixture of data:

writer.writerow([sub.encode("utf-8")  if isinstance(sub, basestring) else sub for sub in rowPrinter[x]])
Sign up to request clarification or add additional context in comments.

4 Comments

AttributeError: 'int' object has no attribute 'encode'
@LucasCrostarosa, so you have a mixture of strings and ints?
yea, for example a is an int, b is a timestamp, and c is a string
Then the edit will work, you only want to encode the strings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.