469,366 Members | 2,045 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

subprocess stdin encoding

yc
I have a encoding problem during using of subprocess. The input is a
string with UTF-8 encoding.

the code is:

tokenize =
subprocess.Popen(tok_command,stdin=subprocess.PIPE ,stdout=subprocess.PIPE,close_fds=True,shell=True)

(tokenized_text,errs) = tokenize.communicate(t)

the error is:
File "/usr/local/python/lib/python2.5/subprocess.py", line 651, in
communicate
return self._communicate(input)
File "/usr/local/python/lib/python2.5/subprocess.py", line 1115, in
_communicate
bytes_written = os.write(self.stdin.fileno(), input[:512])
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in
position 204: ordinal not in range(128)
How I change the default encoding from "ascii" to "utf-8"?

Ying Chen

Feb 5 '07 #1
1 4668
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

yc wrote:
I have a encoding problem during using of subprocess. The input is
a string with UTF-8 encoding.

the code is:

tokenize =
subprocess.Popen(tok_command,stdin=subprocess.PIPE ,stdout=subprocess.PIPE,close_fds=True,shell=True)
(tokenized_text,errs) = tokenize.communicate(t)

the error is: File "/usr/local/python/lib/python2.5/subprocess.py",
line 651, in communicate return self._communicate(input) File
"/usr/local/python/lib/python2.5/subprocess.py", line 1115, in
_communicate bytes_written = os.write(self.stdin.fileno(),
input[:512]) UnicodeEncodeError: 'ascii' codec can't encode
character u'\xa9' in position 204: ordinal not in range(128)
How I change the default encoding from "ascii" to "utf-8"?

Ying Chen
find code like

def setencoding():
"""Set the string encoding used by the Unicode implementation. The
default is 'ascii', but if you're willing to experiment, you can
change this."""
encoding = "ascii" # Default value set by _PyUnicode_Init()
if 0:
# Enable to support locale aware default string encodings.
import locale
loc = locale.getdefaultlocale()
if loc[1]:
encoding = loc[1]

in site.py . and change if 0: to if 1: to enable string encoding.
Now, you can execute python interpreter with LC_CTYPE='UTF-8'.
- --
Thinker Li - th*****@branda.to th********@gmail.com
http://heaven.branda.to/~thinker/GinGin_CGI.py
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFxykX1LDUVnWfY8gRAseRAKCjAksq22bD2YoOt5IEOI cwOB2KiQCbBvvw
lEccSfEaeOhzAUbvulnDoDk=
=y4Jj
-----END PGP SIGNATURE-----

Feb 5 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Cameron Laird | last post: by
5 posts views Thread by Grant Edwards | last post: by
9 posts views Thread by Phoe6 | last post: by
12 posts views Thread by bhunter | last post: by
2 posts views Thread by Chuckk Hubbard | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.