In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article to share with you is about python how to solve the coding error UnicodeEncodeError: 'ascii' codec can't encode characters in pos problem, Xiaobian think quite practical, so share with you to learn, I hope you can read this article after harvest, not much to say, follow Xiaobian to see it.
Problem Description:
The same code runs OK on one server, and the other runs with the following error:
File "/data/soft/knowledge_etl/databus-gedai-v1/schedule_job/main.py", line 270, in print ('demo\u4e3a\u5168\u90e8kp_md5_etl\u4e3b\u4efb\u52a1\u4e3a\u4f8b\u5b50') UnicodeEncodeError: 'ascii' codec can't encode characters in position 4-6: ordinal not in range(128) 1-Location str.decode ('utf-8')#decode is used to convert strings encoded in other codes into unicode encodings #python2s = u'Chinese's.decode ('utf-8') print s#Chinese #python3#Because in python3, all strings are encoded in Unicode, str type is uniformly used to save, and str type has no decode(decode) method. So it's called Traceback.(most recent call last): File "tmp_2.py", line 4, in s.decode('utf-8')AttributeError: 'str' object has no attribute 'decode'#str type has encode(encoding) method #encode is used to convert unicode encoding to other encoded strings s1=u'halo'print (s1.encode ('utf-8'))#b'\xe5\x93\x88\xe5\x92\xaf' #output encoding unicode result 2-positioning character encoding problem #open python at terminal>>a = b'\xe5\x94\xb1\xe6\xad\x8c'#unicode encoding, Note here that a is not a string>>>a = a.decode("utf-8")#decode>>print(a) sing #exclude character encoding and code errors 3-locate print problem-this is the problem! import sysprint(sys.stdout.encoding)#Display: US-ASCII#Now find the problem, is the editor environment uses US-ASCII encoding, so there will be errors ##This method should also be used to view the output encoding of print
Cause analysis: Sometimes the locale of a process is set to support only ASCII characters (e.g. LANG=C). Python will then set the standard output and standard error encoding to ascii, causing the output to be incorrect in Chinese. In the Visual Studio Code editor here, it is set to ascii code, causing the output Chinese error.
import sysimport iodef setup_io():sys.stdout = sys.__ stdout__ = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8', line_buffering=True)sys.stderr = sys.__ stderr__ = io.TextIOWrapper(sys.stderr.detach(), encoding='utf-8', line_buffering=True)setup_io()#sys.stdout is an io.TextIOWrapper with a buffer attribute containing an io. Buffered Writer. We use it to make a new io.TextIOWrapper, specifying the encoding as UTF-8. In addition to encoding, error handling and buffering can also be set here. So this trick can also be used to tolerate coding errors and change the buffering of standard outputs. s1='I miss you'print(s1)#I miss you ~ correct The above is how python solves the coding error UnicodeEncodeError: 'ascii' codec can't encode characters in pos problem, Xiaobian believes that some knowledge points may be seen or used 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.