Python uses subprocess to get command line input after Chinese transcoding problem

Source: Internet
Author: User

Python uses the subprocess module to easily run system commands, and to get input, but when the output of the content of Chinese when the returned data is encoded, not normal display, so it is necessary to transcode, the process is as follows:

First of all, you should know the command to use which encoding to display the output results, run the chcp command on the command line to get the active page code, such as: "active page code: 936"

MS-DOS provides character sets for the following countries and languages:
Code page description
1258 Vietnamese
1257 the Baltic language
1256 Arabic
1255 Hebrew
1254 Turkish
1253 Greek
1252 Latin 1 characters (ANSI)
1251 Cyrillic language
1250 Central European languages
950 Traditional Chinese
949 Korean
936 Simplified Chinese (default) GBK
932 Japanese
874 Thai
850 Multi-lingual (MS-DOS Latin1)
437 MS-DOS American English
65001 UTF-8

In the PY, the obtained results can be transcoded accordingly.

For example, my Activity code page is 936, which is GBK encoding

Then it should be written like this:

String = str (Output.stdout.readline (), encoding='gbk')


Output returns an object for the Subprocess.popen command

Python uses subprocess to get command line input after Chinese transcoding problem

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.