Urlencode and urldecode in Python

Source: Internet
Author: User
Tags url decode

This is normal when the URL address contains Chinese characters or the parameter has Chinese characters, but when such a URL is passed as a parameter (the most common callback ), encode and convert some Chinese characters or even.

1. urlencode

There is a urlencode function in the urllib library, which can convert key-value pairs such as key-value into the desired format and return strings such as a = 1 & B = 2, for example:

 

>>>From UrllibImport Urlencode

>>> Data= {

...'A':'Test',

...'Name':'Warcraft'

...}

>>>Print Urlencode (data)

A=Test & name=%C4%A7%CA%De

What if you only want to perform urlencode conversion on a string? Urllib provides another function: quote ()

>>>From UrllibImport Quote

>>> Quote ('Warcraft')

'% C4 % A7 % Ca % de'

Ii. urldecode

When the string after urlencode is passed, the URL decode will be decoded after acceptance. Urllib provides the unquote () function, and there is no urldecode ()!

>>>From UrllibImport Unquote

>>> Unquote ('% C4 % A7 % Ca % de')

'\ Xc4 \ Xa7 \ xca \ xde'

>>>Print Unquote ('% C4 % A7 % Ca % de')

Warcraft

Iii. Discussion

When urldecode is used, the output of the unquote () function corresponds to the encoding of Chinese Characters in GBK. It is not difficult to find out the result of comparing quote, the so-called urlencode is to convert the string into GBK encoding, and then replace \ x with %. If your terminal is utf8 encoded, convert the result to utf8 output. Otherwise, garbled characters will occur.

You can customize or rewrite functions such as urlencode () and urldecode () according to the actual situation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.