In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Chinese in Python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Chinese in Python".
Using Chinese in Python
It has two default strings: str and unicode. When using it, you must pay attention to the difference between "Unicode string" and "unicode object". All subsequent "unicode strings" refer to "unicode objects" in python.
In fact, there is no such thing as "Unicode string" in Python, only "unicode" object. A traditional unicode string can be represented by a str object. It's just that at this time it's just a byte stream, and it doesn't have any practical meaning unless it's decoded to a unicode object.
We use "" to test on multiple platforms, and the different codes corresponding to "" are:
1. UNICODE (UTF8-16), C854; 2. UTF-8, E59388; 3. GBK, B9FE.
Windows console
The following is the result of running on the windows console:
You can see that in the console, the encoding of Chinese characters is GBK rather than UTF-16. After decoding the string s (GBK encoding) with decode, you get the equivalent unicode object.
Note: just because ss can be printed on the console does not mean it can be serialized directly, such as:
Outputting ss directly to a file throws the same exception. When dealing with a string in unicode, you must first call the encode function on it and convert it to other encoded output. This is the same for all environments.
Summary: in Python, a "str" object is a byte array, and it doesn't matter whether the contents are a legal string or what encoding the string uses (gbk, utf-8, unicode).
These contents need to be recorded and judged by the user. These restrictions also apply to "unicode" objects. Keep in mind that the content of the "unicode" object is definitely not necessarily a legal unicode string, as we'll see soon.
Summary: gbk-encoded str objects and unicode-encoded unicode objects are supported on the windows console.
Analysis of Python Global variable in Python
Give a detailed explanation on the Chinese problems of Python
Sharing of specific Application skills of Python Chinese characters
Adaptation and processing of related documents in Python
How to convert Python into Chinese
Windows IDLE (running on Shell)
In IDLE under windows, the running effect is not exactly the same as that of the windows console:
As you can see, for strings that are not identified by "u", IDLE encodes the Chinese characters in GBK. But for the use of "u" unicode string, IDLE is also using GBK encoding, the difference is that every character is a unicode (object) character! At this point len (ss) = 4. This creates a magical problem that the current ss does not display properly in IDLE. And I have no way to convert ss into normal coding!
For example, use the following methods:
This may be because the localization of IDLE is not good enough and there is a problem with the support for Chinese. It is recommended that you do not use u "Chinese" in IDLE's SHELL, because what you want is not what you want.
This also shows that IDLE's Shell supports two formats of Chinese strings: GBK-encoded "str" objects and UNICODE-encoded unicode objects.
Thank you for your reading, the above is the content of "how to use Chinese in Python", after the study of this article, I believe you have a deeper understanding of how to use Chinese in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.