Analysis on the correct use of Python print

Source: Internet
Author: User

The Python programming language is a relatively novel programming language. Compared with other languages, it has many different features that have aroused the interest of most developers. Here, we can analyze the related application methods of Python print to get a preliminary understanding of the application methods of this language.

Python print automatically encodes the output text, but does not write the file object. Therefore, when some strings are output normally using print, write to file is not necessarily the same as print. The encoding of print conversion is related to environment variables. Windows XP converts data to gbk. In linux, it is converted according to environment variables. Use the locale command in linux. For example, mine is:

 
 
  1. [zhaowei@papaya zhaowei]$ locale  
  2. LANG=zh_CN 
  3. LC_CTYPE="zh_CN" 
  4. LC_NUMERIC="zh_CN" 
  5. LC_TIME="zh_CN" 
  6. LC_COLLATE="zh_CN" 
  7. LC_MONETARY="zh_CN" 
  8. LC_MESSAGES="zh_CN" 
  9. LC_PAPER="zh_CN" 
  10. LC_NAME="zh_CN" 
  11. LC_ADDRESS="zh_CN" 
  12. LC_TELEPHONE="zh_CN" 
  13. LC_MEASUREMENT="zh_CN" 
  14. LC_IDENTIFICATION="zh_CN" 
  15. LC_ALL= 

At this time, it will be considered gb2312. In python, you can use the locale module to obtain the encoding of the current environment:

 
 
  1. import locale  
  2. print locale.getdefaultlocale() 

Python print will automatically replace the string with this encoding during output. Let's take a look at the following. The word "Taobao" is a well-known word that is not found in gb2312. When you convert it to gb2312, an error will occur.

 
 
  1. #-*-Encoding: gb18030 -*-
  2. Import locale
  3. Import sys, encodings, encodings. aliases
  4. # Now a is unicode
  5. A = u'hangzhou'
  6. Print a. encode ("gb2312 ")

The above code reports an exception, which is the cause. But if it is print a directly, it can be output to assume that your environment variable is GBK, GB18030 or UTF-8 ). If your environment variable is GB2312, this print will report an error! So when processing text data from other places, it is best not to use GB2312 encoding, Chinese data, must use GB18030 or UTF-8!

Writing unicode data with the write of the file object will also lead to errors! Encoding conversion required

 
 
  1. #-*-Encoding: gb18030 -*-
  2. Import locale
  3. Import sys, encodings, encodings. aliases
  4. # Now a is unicode
  5. A = u'hangzhou'
  6. F = open ("aaa.txt", "w ")
  7. F. write ()
  8. F. close ()

The above is our introduction to Python print.

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.