LA_
LA_

Reputation: 20409

How to URL encode Chinese characters?

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?

Upvotes: 4

Views: 9804

Answers (1)

Kxrr
Kxrr

Reputation: 520

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)

Upvotes: 6

Related Questions