subprocess stdin encoding

Thinker thinker at branda.to
Mon Feb 5 07:54:48 EST 2007


-----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 - thinker at branda.to thinker.li at 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

iD8DBQFFxykX1LDUVnWfY8gRAseRAKCjAksq22bD2YoOt5IEOIcwOB2KiQCbBvvw
lEccSfEaeOhzAUbvulnDoDk=
=y4Jj
-----END PGP SIGNATURE-----




More information about the Python-list mailing list