Closed ben-en closed 8 years ago
I'm afraid that I'm not able to reproduce this. Here's my terminal, with my attempt, perhaps you can see where I did things differently:
$ pip install unicodecsv
Collecting unicodecsv
Installing collected packages: unicodecsv
Successfully installed unicodecsv-0.14.1
$ python
Python 3.5.0 (default, Sep 14 2015, 09:14:42)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import io
>>> f = io.BytesIO()
>>> import unicodecsv as csv
>>> writer = csv.writer(f)
>>> row = ['http://localhost:10081/content/HiMFYeCsAfJxP9Jc/allianceearth.org', 'South African team may have solved solar puzzle even Google couldn’t crack', 'ARL', 'ephemeral', 'rss_feeder', '2015-11-30 21:53:07 ', '', '']
>>> writer.writerow(row)
192
>>> f.seek(0)
0
>>> f.readlines()
[b'http://localhost:10081/content/HiMFYeCsAfJxP9Jc/allianceearth.org,South African team may have solved solar puzzle even Google couldn\xe2\x80\x99t crack,ARL,ephemeral,rss_feeder,2015-11-30 21:53:07 ,,\r\n']
>>>
Ah, you know what, I'll bet you're opening the file that you're writing to in text mode. unicodecsv
must write to a binary mode file, because unicodecsv
takes care of encoding all the data to bytes.
Hm, interesting. It does seem like that would be the most likely culprit. I'll have a look at that, thank you.
That was the solution! I was using a StringIO object. Thanks again!
To make extra certain that all of my items in the row i was submitting were in unicode, i also called the following function on all of them:
I'm not sure if i'm providing an incorrect argument, but i'd be happy to provide any further information to assist in determining what exactly is going wrong.