can't encode character u'\xa4'

A forum for general discussion of the Python programming language.

can't encode character u'\xa4'

Postby ylafont » Fri Apr 04, 2014 12:07 am

When i pipe the results of a script to a file, i get the error below. Without the pipe the scrips works fine.

I search for potential Unicode problems but did not find anything that pointed me in the right direction.


Code: Select all
xbmc@PlexOne:/mnt/LocalC/temp$ python xmltv.bak.py >  /tmp/temp.xls
Traceback (most recent call last):
  File "xmltv.bak.py", line 30, in <module>
    print ChannelID,'\t', '\t' .join (ChannelInfo.text for ChannelInfo in Channel,)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa4' in position 34: ordinal not in range(128)



Thank you.
ylafont
 
Posts: 23
Joined: Tue Apr 01, 2014 10:29 pm

Re: can't encode character u'\xa4'

Postby stranac » Fri Apr 04, 2014 9:55 am

When you want to write unicode data to a file, you have to tell python what encoding to use(by default it's ascii).
You have two options:
  • manually encode before writing to file
  • use codecs.open() with appropriate encoding(on python 3, you can just use built-in open this way)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1155
Joined: Thu Feb 07, 2013 3:42 pm

Re: can't encode character u'\xa4'

Postby ylafont » Sat Apr 05, 2014 3:37 am

This helped. thank you. I also think my problem had to do with executed python3 instead of just python.

also this page also had a good explanation as well


http://chimera.labs.oreilly.com/books/1 ... /ch05.html

thanks again.
ylafont
 
Posts: 23
Joined: Tue Apr 01, 2014 10:29 pm


Return to General Discussions

Who is online

Users browsing this forum: W3C [Linkcheck] and 4 guests