In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares to you is about how to carry on the analysis of Python Chinese garbled code. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Create a file test.txt, the file format is ANSI, and the content is: abc Chinese is read by python
# coding=gbk print open ("Test.txt"). Read ()
Result: abc Chinese to change the file format to UTF-8 here need to be decoded:
# coding=gbk import codecs print open ("Test.txt"). Read (). Decode ("utf-8")
Result: I used Editplus to edit the above test.txt in abc, but when I edited and saved it into UTF-8 format with the notepad that came with Windows, I ran the Times wrong:
Traceback (most recent call last):
File "ChineseTest.py", line 3, in
Print open ("Test.txt"). Read (). Decode ("utf-8")
UnicodeEncodeError: 'gbk' codec can't encode
Character u'\ ufeff' in position 0: illegal multibyte
Sequence
It turns out that when some software, such as notepad, saves a file encoded in UTF-8, it inserts three invisible characters (0xEF 0xBB 0xBF, or BOM) at the beginning of the file. So we need to remove these characters ourselves when reading, and the codecs module in python defines this constant:
# coding=gbk import codecs data = open ("Test.txt"). Read () if data [: 3] = = codecs.BOM_UTF8: datadata = data [3:] print data.decode ("utf-8")
Results: abc Chinese
The above is how to analyze the problem of garbled codes in Python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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.