Unicode error

Thomas Jollans thomas at jollans.com
Fri Jul 23 12:27:47 EDT 2010


On 07/23/2010 12:56 PM, dirknbr wrote:
> To give a bit of context. I am using twython which is a wrapper for
> the JSON API
> 
>  
> search=twitter.searchTwitter(s,rpp=100,page=str(it),result_type='recent',lang='en')
>         for u in search[u'results']:
>             ids.append(u[u'id'])
>             texts.append(u[u'text'])
> 
> This is where texts comes from.
> 
> When I then want to write texts to a file I get the unicode error.

So your data is unicode? Good.

Well, files are just streams of bytes, so to write unicode data to one
you have to encode it. Since Python can't know which encoding you want
to use (utf-8, by the way, if you ask me), you have to do it manually.

something like:

outfile.write(text.encode('utf-8'))




More information about the Python-list mailing list