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

How to use python to read text files in utf-8 encoding format

2025-04-13 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 "how to use python to read text files in utf-8 encoding format". In the operation of actual cases, many people will encounter such a dilemma, so 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!

# read the text file in utf-8 encoding format # here the Python interpreter reads the byte stream encoded by utf-8, and then interprets these byte streams in the specified encoding mode # this makes it easier to understand the cause of garbled code # coding=utf-8 # When Python reads the encoding it tries to interpret the file as utf-8 # tells the Python interpreter which encoding method to use when compiling # if the encoding method is not set And the interpreter can recognize the file (for example, some files in utf-8 encoding format (related to the editor) have BOM, which can be recognized by the interpreter), then the file encoding method is adopted. Instead, use the terminal default encoding method import sys# reload (sys) # specify the terminal default encoding method # sys.setdefaultencoding ('utf8') # get the terminal default encoding method # sys.getdefaultencoding () import codecsdef ConvertCN (s): if s [: 3] = = codecs.BOM_UTF8: s = s [3:] # if the system default encoding formula is acsii and the default encoding method has not been modified # s.encode ('gbk'): s (file encoding format)-(.decode (' ascii'))-> unicode-- (.encode ('gbk'))-> s (gbk encoding format) (this process is completed by the terminal If no encoding method is specified, the terminal default encoding method will be used) # compatibility: ASCII-- > ISO-8859-1-- > UNICODE (UTF-8,UTF-16,UTF-32)-- > UCS (UCS2 [compatible with UNICODE], UCS4), ASCII-- > GB2312-- > GBK-- > GB18030 # because utf-8 is backward compatible, ascii ascii is not upward compatible with utf-8. Error return s.decode ('utf-8') def PrintFile (filename): F = file (filename) may occur when ascii decoding s (utf-8 encoding format) here to unicode 'r') for f_line in f.readlines (): print ConvertCN (f_line) f.close () if _ _ name__ = = "_ _ main__": PrintFile ('OperCodingFile.txt') # Python outputs byte stream printing by the terminal processing # print display at the terminal is determined by the terminal "" it roughly explains the print principle in python: When Python executes a print statement It simply passes the output to the operating system (using fwrite () or something like it), and some other program is responsible for actually displaying that output on the screen. For example, on Windows, it might be the Windows console subsystem that displays the result. Or if you're using Windows and running Python on a Unix box somewhere else, your Windows SSH client is actually responsible for displaying the data. If you are running Python in an xterm on Unix, then xterm and your X server handle the display. To print data reliably, you must know the encoding that this display program expects. Simply put, the print in python passes the string directly to the operating system, so you need to decode the str into a format consistent with the operating system. Windows uses CP936 (almost the same as gbk), so you can use gbk here. "" print ConvertCN (\ n* Press any key to exit! * ") sys.stdin.readline ()" how to use python to read text files in utf-8 encoding format "is introduced here. Thank you for your 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