Strings and Unicode

John Machin sjmachin at lexicon.net
Sun Jul 20 09:47:52 EDT 2003


madsurfer2000 at hotmail.com (-) wrote in message news:<fef0a228.0307200027.2ef962e9 at posting.google.com>...
> I have a function that takes a string as an input parameter. This
> function then urlencodes the string and sends it to a server with
> telnetlib.Telnet
> 
> The problem is that the string gets converted into what seems to be
> Unicode. How can I check to see if the input-string is Unicode and
> convert it to a different character set (in this case ISO-Latin1).

You don't/can't urlencode a string. You urlencode a bunch of
(key,value) strings which are represented as "a mapping object or a
sequence of two-element tuples" so sayeth the documentation. See
below. Passing a string attracts a TypeError. See below. If you
provide a simple example of what you are doing, plus the output that
"seems to be Unicode", plus of course the input that cause the strange
output, we may be able to help you further.

Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on
win32
>>> import urllib
>>> foo = urllib.urlencode({'foo':42,'bar':69})
>>> type(foo)
<type 'str'>
>>> foo
'foo=42&bar=69'
>>> foo = urllib.urlencode('abcdef')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "c:\python22\lib\urllib.py", line 1171, in urlencode
    raise TypeError
TypeError: not a valid non-string sequence or mapping object
>>>




More information about the Python-list mailing list