problem
In the usual work, encountered such a mistake:
Unicodedecodeerror: ' ASCII ' codec can ' t decode byte
Presumably everyone has met, very common. So I decided to do a collation and study of Python coding.
Basic knowledge
In python2.x, there are two types of data, Unicode and STR, both of which are subclasses of basestring
>>> a = ' Medium ' >>> type (a)
>>> isinstance (a,basestring) true
This problem is very strange, the server executes a write database Python file, normal execution, but the use of Java SSH to invoke the script, the discovery is not to execute the database write, and then use the way of try except, printed the error message, found the errors:' ASCII ' codec can ' t encode characters in position 0-15:ordinal not in range (128)Solution:Add the following 3 words to the Python
numeric Type- int, float, long, complex There are four different numeric types: ordinary integers, long integers, floating-point numbers, and complex numbers A normal integer (or short integer) is implemented using a long in C with a precision of at least 32 bits (Sys.maxint is always set to the maximum normal integer value of the current platform, and the minimum value is -sys.maxint - 1). long integers have infinite precision. floating-point numbers are usually implemented with a double in C
When using a function, we should first consider it at the object level. it is not a special thing. although we have used a lot of space to talk about it, it is still an object. There are always a few things about functions. Below are some notes for writing functions. In particular, I did not summarize these issues. I copied them from a book named "Learning Python" and wrote them in Chinese by the way. of co
Web development based on hi-nginx (python) -- UTF-8 coding, hi-nginxutf-8
It is very important to use UTF-8 encoding in a consistent manner. This is especially true for python2.
If you are using python2 in hi-nginx and you need to use Chinese text such as Japan, Korea, and so on without any difficulty, do not forget to use it:
#-*-Coding: UTF-8 -*-
At the sam
Camp had a lot of holes in the Python2 code.Understand the coding of pyhon2There are only two types of Python2 string: str type: b ' XXX ' is also a str type, which is the encoded type, Len () computes the Unicode type in bytes: Len () is calculated by Unicode charactersPackage Codecs.open, Codecs.write can specify the encodingCode for Python3Python3 There are two types of string: str type: U ' XXX ' is also a str type, both of which are not encoded u
PS: The list of books is arranged in the order of publication time.Learn how Python is not clearly recommended to join the Exchange groupNo.: 548377875The group has like-minded small partners, mutual help,There are good learning tutorials in the group!Saito XiaTranslator: Lu Yujie
Japan Deep Learning Introduction Classic bestseller, the original listing less than 2 years printing has reached 100 000 volumes
Long-term ranked in the "AI" ca
Module Name:Lowercase letters, dividing between words with _ad_stats.pyPackage Name:Same as the module nameClass Name:Capitalize the first letter of the wordAdstatsConfigutilglobal variable names (class variables, equivalent to static variables in Java):Uppercase letters, dividing between words with _NumberColor_writeCommon variables:Lowercase letters, dividing between words with _This_is_a_varInstance variable:Start with _, the rest is the same as the normal variable_price_instance_varCommon fu
Python programs have different support for Unicode codesPython3 support is good , add the following code at the beginning of the file to work1 # -*-coding:utf-8-*-A simple example is as follows:1 # -*-coding:utf-8-*- 2 3 Print ('hellow world\n') 4 Print ('hellow Again')If you do not include the above code, you will be prompted for unresolved charactersPython2 is defective , even if added to the above code, Chinese in the cmd output will still appear g
steps are as follows:
1. set the encoding position to the start of the input stream.
2. find the maximum matching string in the search area in the awaiting coding area of the sliding window
3. if the string is found, the output (offset value, matching length), and the window slides forward, "matching length"
4. if not found, the output (0, 0, the first character of the encoding area), the window slides forward to a unit.
5. if the encoding area is no
Python Chinese display garbled, already at the beginning with coding:utf-8 or garbled.In the python2.7 will garbled, in Python3 not, so it is fixed in the compiler pycharm problem.Here's how to fix it:1. Open the Settings icon in toolbar650) this.width=650; "title=" QQ picture 20170207230145.png "Src=" http://s5.51cto.com/wyfs02/M00/8D/61/ Wkiom1iz4x2chowyaab7msdlapq511.png-wh_500x0-wm_3-wmp_4-s_2044916945.png "alt=" Wkiom1iz4x2chowyaab7msdlapq511.png
PEP8 Python Coding Specification One code orchestration
1 indent. Indentation of 4 spaces (the editor can do this), no tap, no mix of taps and spaces.2 The maximum length per line is 79, the line break can use backslashes, preferably with parentheses. The newline point is to hit enter after the operator.There are two empty lines between the 3 class and the top-level function definition, an empty li
PEP8 Python Coding SpecificationA code orchestration1 indent. Indentation of 4 spaces (the editor can do this), no tap, no mix of taps and spaces.2 The maximum length per line is 79, the line break can use backslashes, preferably with parentheses. The newline point is to hit enter after the operator.There are two empty lines between the 3 class and the top-level function definition, an empty line between th
As the title, I use Python to parse the file (the file contains Chinese) and write the other file times incorrectly:
Unicodeencodeerror: ' ASCII ' codec can ' t encode characters
Through the guidance of the Google Great God, determine the following solutions:
#coding: Utf-8import sys Reload (SYS) sys.setdefaultencoding (' Utf-8 ')
Question: Why do we need reload?
Grep-r-i ' setdefaultencod
'))W.Close()# We'll read it out again #R= Open(' Demo.txt ', ' R ')content= R.Read()R.Close()# by Repr and type checking, we can see the encoded string of the read or str type# without being converted into a UNICDE stringPrint' repr content: ', repr(content)Print' type of content: ', type(content)Print' content: ', content# Summary below:The # Open () function reads data from disk in bytes, and gets the text of str type,# If a byte is not a asccii code character, it is represented by an escaped
The encoding and decoding in Python is the conversion between Unicode and Str. Encoding is Unicode-STR, instead, decoding is str---Unicode. The rest of the problem is deciding when to encode or decode. The "code indication" at the beginning of the file, which is the #-*-coding:-*-this statement. The Python default script file is UTF-8 encoded and is corrected wit
PEP8 Specification Official Document: https://www.python.org/dev/peps/pep-0008/PEP8 Chinese Translation: http://www.cnblogs.com/ajianbeyourself/p/4377933.html the following from Https://www.douban.com/note/134971609/PEP8 Python Coding SpecificationA code orchestration1 indent. Indentation of 4 spaces (the editor can do this), no tap, no mix of taps and spaces.2 The maximum length per line is 79, the line br
Recently, "Ruili model learning Python" hotspot has affected the hearts of programmers across the north and south, some people say this is speculation, some people say this is to promote the point of Python ... I am a smile, beauty is a beauty, vision that is a lever, not only the United States, learning the language is also beautiful. That we can not hide private, I must have read the
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.