English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes a simple method for detecting text types in Python. Share it with everyone for reference, as follows:
1According to the file header.
#Whether it is UTF with BOM header8File def IsUtf8BomFile(pathfile): if b'\xef\xbb\xbf' == open(pathfile, mode='rb').read(3)): return True return False
2Use the cchardet library.
>>> import cchardet >>> cchardet.detect(open(pathfile, 'rb').read()) {'encoding': 'UTF-8', 'confidence': 0.9900000095367432{}
Readers who are interested in more content related to Python can check the special topics on this site: 'Summary of Python File and Directory Operation Skills', 'Summary of Python Text File Operation Skills', 'Summary of Python URL Operation Skills', 'Summary of Python Image Operation Skills', 'Python Data Structures and Algorithms Tutorial', 'Summary of Python Socket Programming Skills', 'Summary of Python Function Usage Skills', 'Summary of Python String Operation Skills', and 'Classic Tutorial of Python Entry and Advanced Learning'.
I hope the content described in this article will be helpful to everyone in designing Python programs.
Declaration: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.