Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83731 > unrolled thread

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

Started byPeng Yu <pengyu.ut@gmail.com>
First post2015-01-13 21:26 -0600
Last post2015-01-13 21:26 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  UnicodeEncodeError: 'ascii' codec can't encode character u'\ua000' in position 0: ordinal not in range(128) Peng Yu <pengyu.ut@gmail.com> - 2015-01-13 21:26 -0600

#83731 — UnicodeEncodeError: 'ascii' codec can't encode character u'\ua000' in position 0: ordinal not in range(128)

FromPeng Yu <pengyu.ut@gmail.com>
Date2015-01-13 21:26 -0600
SubjectUnicodeEncodeError: 'ascii' codec can't encode character u'\ua000' in position 0: ordinal not in range(128)
Message-ID<mailman.17699.1421205984.18130.python-list@python.org>
Hi,

I am trying to understand what does encode() do. What are the hex
representations of "u" in main.py? Why there is UnicodeEncodeError
when main.py is piped to xxd? Why there is no such error when it is
not piped? Thanks.

~$ cat main.py
#!/usr/bin/env python

u = unichr(40960) + u'abcd' + unichr(1972)
print u
~$ cat main_encode.py
#!/usr/bin/env python

u = unichr(40960) + u'abcd' + unichr(1972)
print u.encode('utf-8')
$ ./main.py
ꀀabcd޴
~$ cat main.sh
#!/usr/bin/env bash

set -v
./main.py | xxd
./main_encode.py | xxd

~$ ./main.sh
./main.py | xxd
Traceback (most recent call last):
  File "./main.py", line 4, in <module>
    print u
UnicodeEncodeError: 'ascii' codec can't encode character u'\ua000' in
position 0: ordinal not in range(128)
./main_encode.py | xxd
0000000: ea80 8061 6263 64de b40a                 ...abcd...

-- 
Regards,
Peng

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web