subprocess stdin encoding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yc

    #1

    subprocess stdin encoding

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

    the code is:

    tokenize =
    subprocess.Pope n(tok_command,s tdin=subprocess .PIPE,stdout=su bprocess.PIPE,c lose_fds=True,s hell=True)

    (tokenized_text ,errs) = tokenize.commun icate(t)

    the error is:
    File "/usr/local/python/lib/python2.5/subprocess.py", line 651, in
    communicate
    return self._communica te(input)
    File "/usr/local/python/lib/python2.5/subprocess.py", line 1115, in
    _communicate
    bytes_written = os.write(self.s tdin.fileno(), input[:512])
    UnicodeEncodeEr ror: '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

  • Thinker

    #2
    Re: subprocess stdin encoding

    -----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.Pope n(tok_command,s tdin=subprocess .PIPE,stdout=su bprocess.PIPE,c lose_fds=True,s hell=True)
    >
    >
    (tokenized_text ,errs) = tokenize.commun icate(t)
    >
    the error is: File "/usr/local/python/lib/python2.5/subprocess.py",
    line 651, in communicate return self._communica te(input) File
    "/usr/local/python/lib/python2.5/subprocess.py", line 1115, in
    _communicate bytes_written = os.write(self.s tdin.fileno(),
    input[:512]) UnicodeEncodeEr ror: '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.getdefau ltlocale()
    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@branda. to thinker.li@gmai l.com

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.6 (FreeBSD)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFFxykX1LD UVnWfY8gRAseRAK CjAksq22bD2YoOt 5IEOIcwOB2KiQCb Bvvw
    lEccSfEaeOhzAUb vulnDoDk=
    =y4Jj
    -----END PGP SIGNATURE-----

    Comment

    Working...