UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128)

akhil1988 akhilanger at gmail.com
Fri Jul 17 05:26:39 CEST 2009


Well, you were write: unintentionally I removed strip(). But the problem does
not ends here:

I get this error now:

 File "./temp.py", line 488, in <module>
    main()
  File "./temp.py", line 475, in main
    for line in sys.stdin:
  File "/usr/local/lib/python3.1/codecs.py", line 300, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: invalid
data

for this line:
â


--Akhil

Nobody-38 wrote:
> 
> On Thu, 16 Jul 2009 15:43:37 -0700, akhil1988 wrote:
> 
>>> In Python 3 you can't decode strings because they are Unicode strings
>>> and it doesn't make sense to decode a Unicode string. You can only
>>> decode encoded things which are byte strings. So you are mixing up byte
>>> strings and Unicode strings.
>>
>> Then, how should I do it?
>> I read a byte string from sys.stdin which needs to converted to unicode
>> string for further processing.
> 
> In 3.x, sys.stdin (stdout, stderr) are text streams, which means that they
> read and write Unicode strings, not byte strings.
> 
>> I cannot just remove the decode statement and
>> proceed?
>> 
>> This is it what it looks like:
>> 
>> for line in sys.stdin:
>>     line = line.decode('utf-8').strip()
>>     if line == '<page>': #do something here
>>     elsif #do something here
>>     
>> If I remove the decode statement, line == '<page>' never gets true. 
> 
> Did you inadvertently remove the strip() as well?
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

-- 
View this message in context: http://www.nabble.com/UnicodeEncodeError%3A-%27ascii%27-codec-can%27t-encode-character-u%27%5Cxb7%27-in-position-13%3A-ordinal-not-in-range%28128%29-tp24509879p24528030.html
Sent from the Python - python-list mailing list archive at Nabble.com.




More information about the Python-list mailing list