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:
- Study coding problems in Python by errors caused by unicode_literals in __future__
- Bill: To solve the Python Chinese processing garbled, first to understand the "character" and "byte" difference
- Http://docs.python.org/2/library/datetime.html#datetime.date.strftime
- Http://docs.python.org/2.7/library/functions.html#getattr
- Http://docs.python.org/2/whatsnew/2.6.html?highlight=bytestring#pep-3112-byte-literals
- Http://www.cnblogs.com/huxi/articles/1897271.html
- 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