Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of Chinese problems in Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "Python Chinese problem example Analysis". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Before we do that, we need to understand that there are two types of strings in Python: a normal string (each character is represented by 8 bits) and an Unicode string (each character is represented by one or more bytes).

They can be converted into each other and have a more comprehensive description, so I won't say anything more here. Take a look at the following code:

#-*-coding:gb2312-*-# must be on the * line or the second line print "- code 1 -" a = "Chinese an I love you" print a print a.find ("I") b = a.replace ("love") "like") print b print "- code 2 -" x = "Chinese an I love you" y = unicode (x, "gb2312") print y.encode ("gb2312") print y.find (u "I") z = y.replace (u "love") U "like") print z.encode ("gb2312") print "- code 3 -" print y

Said that it encountered non-ASCII characters, and let's refer to pep-0263. PEP-0263 (Python Enhancement Proposal) makes it very clear that Python is also aware of the internationalization problem and proposes a solution. According to the above requirements of the proposal, we have the following code:

-code 1-Chinese an I love you 5 Chinese an I like you-code 2-Chinese an I love you 3 Chinese an I like you-code 3Murray- -Traceback (most recent call last): File "G:\ Downloads\ eclipse\ workspace\ p\ src\ hello.py" Line 16, in print y UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range

We can see that by introducing Python Chinese problem statement, we can use Chinese normally, and in code 1 and 2, the console can print out Chinese correctly. However, it is clear that the above code also reflects a number of problems:

1. Code 1 and 2 use different ways to use print. 1 is direct print, while 2 is encoded before print.

2. Code 1 and 2 look for the same character "I" in the same string, and the results are different (5 and 3, respectively)

3. There is an error when printing the unicode string y directly in code 3 (which is why it is encoded first in code 2).

This is the end of the content of "Python Chinese problem Analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report