4

I used the following code to encode parameters list:

params['username'] = user
params['q'] = q
params = urllib.quote(params)

But it doesn't work when q is equal to 香港. The following error is returned:

'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

How should I fix it?

LA_
  • 19,823
  • 58
  • 172
  • 308
  • I think, u need converter unicode to ascii a example `\\u524d`. Or check http://stackoverflow.com/questions/2365411/python-convert-unicode-to-ascii-without-errors – KingRider Jul 26 '16 at 21:59

1 Answers1

6

It seems that you're working on Python 2+.

Cause your question isn't clear enough, I offer a normal way to solve it.

Here's two advice to fix it:

  • add # encoding: utf-8 before your file
  • encode Chinese characters to utf-8 before call quote

Here's example:

 # encoding: utf-8

import urllib


def to_utf8(text):
    if isinstance(text, unicode):
        # unicode to utf-8
        return text.encode('utf-8')
    try:
        # maybe utf-8
        return text.decode('utf-8').encode('utf-8')
    except UnicodeError:
        # gbk to utf-8
        return text.decode('gbk').encode('utf-8')


if __name__ == '__main__':
                # utf-8       # utf-8                   # unicode         # gdk
    for _text in ('香港', b'\xe9\xa6\x99\xe6\xb8\xaf', u'\u9999\u6e2f', b'\xcf\xe3\xb8\xdb'):
        _text = to_utf8(_text)
        print urllib.quote(_text)
Kxrr
  • 506
  • 6
  • 14