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

Nobody nobody at nowhere.com
Thu Jul 16 21:23:41 EDT 2009


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?




More information about the Python-list mailing list