0

I'm trying to write some data in csv file using the binary mode. My data is a list of lists which looks like this:

data= [[None, u'https://aaa.com/p/cat.do?tags=|5555|4444|8888&zzz;A=3', u'00 .00.00.000, 00.00.00.000', u'25359725', u'2018-03-06 18:01:18', u'DC01F54GH8D.aa201', None, 1814498434, 765651, u'2018-03-12 18:01:18', 168, 0, u'2018-03-12 18: 32:08.428032'], [None, u'https://aaa.com/p/cat.do?tags=|5555|4444|8888&zzz;A=\x80\x99s+Day+', u'00.00.00.000, 00.00.00.000', u'10707456', u'2018-03-06 18:01:02', u'76FD86AA.abd', None, 1814498440, 760960, u'2018-03-12 18:01:02', 168, 0, u'2018-03-12 18:32:08.805207']]
filename="table.csv"
with open(filename, "wb") as f:
    writer = csv.writer(f)
    writer.writerows(data)

but this raises the following error:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 152-153: ordinal not in range(128)

I've spent a lot of time trying to solve this problem without success. Can you help me with this please?

I'm using python 2.7.

Thanks in advance

EDIT: I've added the example that is causing the problem + I know that there is a similar post but it only deals with a single string, here I'd like to know how to deal with list of lists.

ABK
  • 511
  • 1
  • 7
  • 19
  • It sounded like an encoding problem (see [here](https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20/9942822)) but it worked for me too. – floatingpurr Apr 12 '18 at 11:20
  • How does "using binary mode" rhyme with the text conversion error that you report? – Jongware Apr 12 '18 at 11:20
  • @Rakesh It's because the data list I posted is just a smple of real data, I can't post all the list because it is very very long and I can't find what is the exact element which causes the error – ABK Apr 12 '18 at 11:21
  • 1
    @usr2564301 that does not answer the user's problem. The OP is not implicitly using `str` – Alastair McCormack Apr 12 '18 at 12:36

1 Answers1

-1

There are multiple ways to solve this :

could you try :

from unidecode import unidecode
new_string = unidecode(string)

then use the new string for your processing

You can also look at this link if unidecode didnt work :

https://stackoverflow.com/questions/3704731/replace-non-ascii-chars-from-a-unicode-string-in-python

Kenstars
  • 662
  • 4
  • 11
  • My data is list of lists I can't apply the unidecode function – ABK Apr 12 '18 at 11:27
  • The code is working for me too , so I am not able to give you a specific solution as it is. I can only suggest you to stringify the list of lists and then run the unidecode and then parse it using json. – Kenstars Apr 12 '18 at 11:34
  • Thanks a lot for your answer, now I added to the post the item which is causing the error – ABK Apr 12 '18 at 12:43