pexpect and unicode strings

Discussion in 'Python' started by Mathew Oakes, Sep 5, 2009.

  1. Mathew Oakes

    Mathew Oakes Guest

    Is there anything that can be done to make pexpect spawns send unicode lines?

    In this example they are just middot characters, but this process
    needs to be able to handle languages in other character sets.

    >>> spokentext = u'Nation . Search the FOX Nation . czars \xb7 Health care \xb7 town halls \xb7 tea parties ...'
    >>> p = pexpect.spawn('festival')
    >>> p.expect('festival> ')

    0
    >>> p.sendline( spokentext )

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python2.6/dist-packages/pexpect.py", line 961, in sendline
    n = self.send(s)
    File "/usr/lib/python2.6/dist-packages/pexpect.py", line 953, in send
    c = os.write(self.child_fd, s)
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in
    position 41: ordinal not in range(128)
    >>>


    Help appreciated!

    cheeers
    Mat
     
    Mathew Oakes, Sep 5, 2009
    #1
    1. Advertising

  2. Mathew Oakes

    Sean DiZazzo Guest

    On Sep 4, 7:11 pm, Mathew Oakes <> wrote:
    > Is there anything that can be done to make pexpect spawns send unicode lines?
    >
    > In this example they are just middot characters, but this process
    > needs to be able to handle languages in other character sets.
    >
    > >>> spokentext = u'Nation . Search the FOX  Nation . czars  \xb7  Health care  \xb7  town halls  \xb7  tea parties  ...'
    > >>> p = pexpect.spawn('festival')
    > >>> p.expect('festival> ')

    > 0
    > >>> p.sendline( spokentext )

    >
    > Traceback (most recent call last):
    >   File "<stdin>", line 1, in <module>
    >   File "/usr/lib/python2.6/dist-packages/pexpect.py", line 961, in sendline
    >     n = self.send(s)
    >   File "/usr/lib/python2.6/dist-packages/pexpect.py", line 953, in send
    >     c = os.write(self.child_fd, s)
    > UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in
    > position 41: ordinal not in range(128)
    >
    >
    >
    > Help appreciated!
    >
    > cheeers
    > Mat


    Did you trying changing line 953 in the pexpect file to:

    c = os.write(self.child_fd, s.encode("utf-8"))

    and then run your code again?

    Worth a shot. I have no good way of trying it out here.

    ~Sean
     
    Sean DiZazzo, Sep 5, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-15?Q?J=F6rg?= Maier

    cygwin python with Tkinter and pexpect module

    =?ISO-8859-15?Q?J=F6rg?= Maier, Sep 6, 2003, in forum: Python
    Replies:
    2
    Views:
    539
    =?ISO-8859-15?Q?J=F6rg?= Maier
    Sep 7, 2003
  2. Adrian Casey

    pexpect and OpenVMS

    Adrian Casey, May 20, 2004, in forum: Python
    Replies:
    2
    Views:
    403
    Cameron Laird
    May 21, 2004
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    766
    Malcolm
    Jun 24, 2006
  4. Asterix
    Replies:
    5
    Views:
    715
    Matt Nordhoff
    Aug 31, 2008
  5. Saqib Ali
    Replies:
    1
    Views:
    362
    Roy Smith
    Dec 26, 2011
Loading...

Share This Page