Issues to note when using the From __future__ import unicode_literals

Source: Internet
Author: User

Add by Zhj: Some libraries in Python require that the parameters must be of type STR, and some interfaces require that the parameter must be a Unicode type string. For the str type string, the call Len () and the traversal, in fact, are in bytes, this is too pit dad, the same character using different encoding format, the length is often different. The string call to the Unicode type Len () and the traversal is in characters, which is what we want. In addition, the interfaces of the Django,django REST Framework are strings that return Unicode types. For the sake of unification, I personally recommend using the from __future__ import unicode_literals to convert all the strings explicitly appearing in the module to the Unicode type, but be aware of where you must use the STR string. About the string type, and it's Python2 's place.

In the py2.7 project with the __future__ module in the unicode_literals to prepare for compatible py3.x, today encountered a unicodeencodeerror error, followed, found this little pit worth noting. What kind of a hole is it? Follow the code to see. By the way, look at the principle.

1. Questions

Unicode_literals not referenced

# Coding:utf-8  from Import  = datetime.now ()print now.strftime ('%M month%d day%h:%m')

This code executes normally, output: March 12 21:53

Introduction of Unicode_literals

# Coding:utf-8  from __future__ Import unicode_literals  from Import  = datetime.now ()print now.strftime ('%M month%d day%h:%m')

Throws the following error:

Traceback (most recent): File "unicode_error_demo2.py", line 7, in <module>      print now.strftime ('%m Month% D-Day%h:%m ') Unicodeencodeerror: ' ASCII ' codec can ' t encode character U ' \u6708 ' in position 2:ordinal not in range (128)

2. Cause analysis

Because Datetime.strftime () accepts only a string of type STR, it does not accept strings of the Unicode type.

3. Solution Plan One (recommended): Incoming parameters for str type
# Coding:utf-8  from __future__ Import unicode_literals  from Import  = datetime.now ()print now.strftime ('%M month%d day%h:%m'. Encode ('utf-8')  #  indicates the str type string

Scenario two (not recommended): Set run-time encoding to Utf-8
 #  coding:utf-8  from  __future__  import   unicode_literals  import   sys  Span style= "color: #0000ff;" >from  datetime import   Datetimereload ( SYS) sys.setdefaultencoding (  utf-8    " ) now  = print  now.strftime (  ) 

References:
    1. Study coding problems in Python by errors caused by unicode_literals in __future__
    2. Bill: To solve the Python Chinese processing garbled, first to understand the "character" and "byte" difference
    3. Http://docs.python.org/2/library/datetime.html#datetime.date.strftime
    4. Http://docs.python.org/2.7/library/functions.html#getattr
    5. Http://docs.python.org/2/whatsnew/2.6.html?highlight=bytestring#pep-3112-byte-literals
    6. Http://www.cnblogs.com/huxi/articles/1897271.html
    7. Http://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal

Issues to note when using the From __future__ import unicode_literals

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.