Python處理文件的方法(mimetypes和chardet)
處理文件時minetype和chardet是很好用的兩個模塊函數(shù):
###chardet:
主要處理文件文件編碼問題

假如有這個一個配置文件,非ascii或者utf8編碼:
__coding__ = 'UTF-8'
__author__ = 'bingo'
import chardet
import configparser
parse = configparser.ConfigParser()
parse.read("config.ini")
print(parse.sections())
運行結果:
G:\Anaconda\python.exe "C:/Users/bingo/Desktop/The crawler/學習/demo.py"
Traceback (most recent call last):
File "C:/Users/bingo/Desktop/The crawler/學習/demo.py", line 29, in <module>
parse.read("config.ini")
File "G:\Anaconda\lib\configparser.py", line 696, in read
self._read(fp, filename)
File "G:\Anaconda\lib\configparser.py", line 1014, in _read
for lineno, line in enumerate(fp, start=1):
UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illegal >multibyte sequence
但是改成下面, 用chardet先獲取文件編碼格式,就可以完美解決上面報錯問題:
import chardet
import configparser
data = open("config.ini", "rb").read()
a = chardet.detect(data)
print(a)
parse = configparser.ConfigParser()
parse.read("config.ini", encoding=a["encoding"])
print(parse.sections())
>>>>>>>>>>>>>>>再運行:
G:\Anaconda\python.exe "C:/Users/bingo/Desktop/The crawler/學習/demo.py"
{'encoding': 'UTF-16', 'confidence': 1.0, 'language': ''}
['config']
Process finished with exit code 0
###mimetypes:
主要處理文件文件類型問題
該模塊提供在文件名或URL與與文件擴展名關聯(lián)的MIME類型之間進行轉換的功能,主要有以下兩個函數(shù):
mimetypes.guess_type(url, strict=True):
返回一個元組(type, encoding), strict默認參數(shù),指定已知MIME類型的列表是否僅限于在IANA注冊的官方類型,type為MIME類型,encoding可能為None
mimetypes.guess_all_extensions(type, strict=True):
返回一個列表,根據傳入的type(MIME類型),返回提供所有可能的文件擴展名的字符串列表,包括前導點('.'),strict默認參數(shù),指定已知MIME類型的列表是否僅限于在IANA注冊的官方類型
import mimetypes
# 獲取文件MIME類型
type, encoding = mimetypes.guess_type("demo.py")
print(type)
# 根據MIME類型獲取所有可能的文件后綴名
c = mimetypes.guess_all_extensions(type)
print(c)
>>>運行結果如下:
G:\Anaconda\python.exe "C:/Users/bingo/Desktop/The crawler/學習/demo.py"
text/plain
['.bat', '.c', '.h', '.ksh', '.pl', '.txt', '.asm', '.cc', '.cod', '.cpp', '.cs', '.csh', '.cshader', >'.csproj', '.cxx', '.def', '.dsh', '.dshader', '.dsp', '.dsw', '.efu', '.filters', '.fx', '.gitattributes', >'.gitignore', '.gitmodules', '.gsh', '.gshader', '.hh', '.hlsl', '.hlsli', '.hpp', '.hsh', '.hshader', >'.hxx', '.i', '.idl', '.inc', '.inl', '.ipp', '.js', '.jsproj', '.jsx', '.jsxbin', '.jsxinc', '.lst', '.mak', >'.map', '.mdp', '.mk', '.odh', '.odl', '.pkgdef', '.pkgundef', '.psh', '.pshader', '.py', '.pyw', >'.rc', '.rc2', '.rct', '.res', '.rgs', '.s', '.sln', '.sol', '.sor', '.srf', '.tlh', '.tli', '.ts', '.tsx', '.tt', >'.user', '.vb', '.vbproj', '.vcp', '.vcw', '.vsh', '.vshader']
.bat
Process finished with exit code 0
到此這篇關于Python—處理文件(mimetypes和chardet)的文章就介紹到這了,更多相關Python—處理文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Django多數(shù)據庫聯(lián)用實現(xiàn)方法解析
這篇文章主要介紹了Django多數(shù)據庫聯(lián)用實現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11
2025最新版Python3.13.1安裝使用指南(超詳細)
Python編程語言自誕生以來,已經成為全球最受歡迎的編程語言之一,它簡單易學易用,以標準庫和功能強大且廣泛外 掛的擴展庫,為用戶提供包羅萬象、強大全面的功能,此次給大家介紹了2025年最新版Python 3.13.1安裝使用指南全面更新,需要的朋友可以參考下2025-03-03
Python利用matplotlib實現(xiàn)餅圖繪制
Pyplot作為Matplotlib的子庫,提供了和MATLAB差不多的繪圖API。因此Pyplot作為常用的繪圖模塊,能很方便讓用戶繪制2D圖表。本文將為大家介紹如何利用Matplotlib繪制餅圖,感興趣的小伙伴可以了解一下2021-12-12

