subprocess stdin encoding

Jean-Paul Calderone exarkun at divmod.com
Mon Feb 5 09:07:25 EST 2007


On 4 Feb 2007 23:10:29 -0800, yc <chenying3176 at gmail.com> 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"?

You don't need to change the default encoding.  You just need to encode
the unicode string you are sending to the child process.  Try:

    tokenized_text, errs = tokenize.communicate(t.encode('utf-8'))

Jean-Paul




More information about the Python-list mailing list