Solve the problem of Python passing Chinese parameters

Source: Internet
Author: User
Today there is a need to pass the Chinese parameter to the URL

But the parameters of the script passing GBK in the GBK environment always give me Unicode decoding errors. It's annoying.

So we choose to use UrlEncode to deal with Chinese,

Because the domestic and foreign website code is different, the country is GBK, the foreign is UTF8.

>>> import sys>>> sys.stdin.encoding ' GBK ' means our environment is GBK >>> import urllib>>> Urllib.quote (' coded pit Daddy ')   '%b1%e0%c2%eb%bf%d3%b5%f9 '

So I got the URL code of GBK, directly to the URL with this code can directly explain the Chinese. We don't need to deal with it anymore.

>>> urllib.quote (' coded pit Daddy ' decode (' GBK '). Encode (' Utf-8 ')) '%e7%bc%96%e7%a0%81%e5%9d%91%e7%88%b9 '

This is the URL code for UTF-8.
Very convenient to solve the problem of another day ...

The above mentioned is the whole content of this article, I hope you can like.

  • 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.