0

I have a large set of data and I am going to export them into a CSV file using the following code.

 with open ('/Users/mz/Dropbox/dis/Programming/zoloft.csv',
            'w', newline = '') as zolo:
        zolo = csv.writer(zolo, delimiter =',', quotechar='|')


        rows = zip(all_rating, all_disorders, all_side_effects,
                   all_comments, all_gender, all_age, all_dosage_duration, all_date)

        for row in rows:       
            zolo.writerow(row)

But there is the following error:

   zolo.writerow(row)
UnicodeEncodeError: 'ascii' codec can't encode character '\xe8' in position 179: ordinal not in range(128)

Is there any way to handle this error inside the code I wrote? Thanks !

Mary
  • 1,142
  • 1
  • 16
  • 37

1 Answers1

2

The csv module, at present, doesn't have Unicode support, but your dataset clearly contains Unicode characters. So what you can do is something similar to the answer to this question, and translate the Unicode characters to their nearest ASCII equivalents (so your text isn't illegible later).

I'd go with something like:

from unidecode import unidecode
with open('file', 'w', newline = '') as zolo:
    zolo = csv.writer(zolo, delimiter =',', quotechar='|')

    rows = zip(all_rating, all_disorders, all_side_effects,
               all_comments, all_gender, all_age,
               all_dosage_duration, all_date)

    for row in rows:       
        zolo.writerow(map(unidecode, row))
Community
  • 1
  • 1
Sebastian Lenartowicz
  • 4,695
  • 4
  • 28
  • 39
  • thank you ! there is still the following error: ' File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/unidecode/__init__.py", line 48, in unidecode_expect_ascii bytestring = string.encode('ASCII') AttributeError: 'tuple' object has no attribute 'encode'', can you help me with that ? – Mary Aug 09 '16 at 09:54
  • thank you !, I tried it. The output is just column of some letters (such as 'o', 'l', 'k') it seems it removed all the information and just kept some letters. – Mary Aug 09 '16 at 15:49
  • @ Sebastian Lenartowicz, Thank you ! – Mary Aug 10 '16 at 08:26