In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to solve the python utf-8 problem, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
Chinese coding problem is often a big problem for programmers who use Chinese, and it is also the case under python, so how to understand and solve the coding problem of python?
We need to know that the internal python uses unicode coding, while the outside has to face all kinds of strange codes, such as gbk,gb2312,utf8, which is often faced by Chinese programs, so how to convert these codes into internal unicode?
First, let's take a look at the use of strings in the source code file. As a text file, the source code file must store the code in some form of encoding. Python defaults to thinking that the source code file is asci encoding, for example, there is a variable assignment in the code:
S _ 1s _ 1
Print s1
Python thinks that this'a'is an asci-encoded character. Everything is fine if only English characters are used, but if Chinese is used, for example:
S1'ha'
Print s1
This code file will make an error when it is executed, but there is something wrong with the coding. By default, python treats the contents of the code file as asci encoding, but there is no Chinese in the asci encoding, so an exception is thrown.
The way to solve the problem is to let python know what encoding form is used in the file. For Chinese, the common encodings that can be used are utf-8,gbk and gb2312. Just add the following at the front end of the code file:
#-*-coding: utf-8
-*-
This is to tell python that the text in my file is encoded in utf-8, so that python will interpret the characters according to the utf-8 encoding and then convert it to unicode encoding for internal processing.
However, if you run this code under the Windows console, although the program is executed, it is not printed on the screen. This is due to the inconsistency between the python code and the console code. Used by the encoding in the console under Windows
It is gbk, and the utf-8,python used in the code is printed to the console of gbk code according to utf-8 code, which will naturally be inconsistent and cannot print out correct Chinese characters.
One solution is to change the coding of the source code to gbk, that is, the first line of the code is changed to:
#-*-coding: gbk
-*-
Another way is to keep the utf-8 of the source file unchanged, but add a u word before "ha", that is:
S 1U'ha'
Print s1
In this way, the word'ha 'can be printed correctly.
The u here means that the following string will be stored in unicode format. Python will recognize the Chinese character'ha'in the code according to the utf-8 code named in the first line of the code, and then convert it into a unicode object. If we use type to look at the data class of'ha'
Type type ('ha'), you get, and type (u'ha'), you get, that is, adding u before the character indicates that this is a unicode object, which exists in memory in unicode format, and if you don't add u
Indicates that this is just a string that uses some kind of encoding, and the encoding format depends on how python recognizes the encoding of the source file, which is utf-8.
When Python outputs a unicode object to the console, it will automatically convert according to the encoding of the output environment, but if the output is not a unicode object but an ordinary string, it will output the string directly according to the encoding of the string, resulting in the above
Elephant.
If you use the unicode object, in addition to using the u tag like this, you can also use the unicode class and the encode and decode methods of the string.
The constructor of the unicode class takes a string parameter and an encoding parameter, encapsulating the string as a unicode. For example, in this case, because we are using utf-8 encoding, the encoding parameters in unicode use 'utf-8' to encapsulate the character as
Unicode object, and then correctly output to the console:
S1=unicode ('ha')
'utf-8')
Print s1
In addition, you can also convert a normal string to a unicode object with the decode function. Many people don't understand what the decode and encode functions of the python string mean. Here is a brief explanation.
Decode parses an ordinary string according to the encoding format in the parameters, and then generates the corresponding unicode object. For example, if our code uses utf-8, the conversion of a string to unicode is as follows:
S2'ha '.decode (' utf-8')
At this point, S2 is a unicode object that stores the word "ha", which is actually the same as unicode ("ha").
Utf-8') and u'ha 'are the same.
Then encode is exactly the opposite function, which converts a unicode object into ordinary characters encoded in the parameter, such as the following code:
S3=unicode ('ha')
'utf-8') .encode (' utf-8')
S3 is now back to the'ha'of utf-8.
The above content is how to solve the python utf-8 problem. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.