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

假如有這個(gè)一個(gè)配置文件,非ascii或者utf8編碼:
__coding__ = 'UTF-8'
__author__ = 'bingo'
import chardet
import configparser
parse = configparser.ConfigParser()
parse.read("config.ini")
print(parse.sections())
運(yùn)行結(jié)果:
G:\Anaconda\python.exe "C:/Users/bingo/Desktop/The crawler/學(xué)習(xí)/demo.py"
Traceback (most recent call last):
File "C:/Users/bingo/Desktop/The crawler/學(xué)習(xí)/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先獲取文件編碼格式,就可以完美解決上面報(bào)錯(cuò)問題:
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())
>>>>>>>>>>>>>>>再運(yùn)行:
G:\Anaconda\python.exe "C:/Users/bingo/Desktop/The crawler/學(xué)習(xí)/demo.py"
{'encoding': 'UTF-16', 'confidence': 1.0, 'language': ''}
['config']
Process finished with exit code 0
###mimetypes:
主要處理文件文件類型問題
該模塊提供在文件名或URL與與文件擴(kuò)展名關(guān)聯(lián)的MIME類型之間進(jìn)行轉(zhuǎn)換的功能,主要有以下兩個(gè)函數(shù):
mimetypes.guess_type(url, strict=True):
返回一個(gè)元組(type, encoding), strict默認(rèn)參數(shù),指定已知MIME類型的列表是否僅限于在IANA注冊的官方類型,type為MIME類型,encoding可能為None
mimetypes.guess_all_extensions(type, strict=True):
返回一個(gè)列表,根據(jù)傳入的type(MIME類型),返回提供所有可能的文件擴(kuò)展名的字符串列表,包括前導(dǎo)點(diǎn)('.'),strict默認(rèn)參數(shù),指定已知MIME類型的列表是否僅限于在IANA注冊的官方類型
import mimetypes
# 獲取文件MIME類型
type, encoding = mimetypes.guess_type("demo.py")
print(type)
# 根據(jù)MIME類型獲取所有可能的文件后綴名
c = mimetypes.guess_all_extensions(type)
print(c)
>>>運(yùn)行結(jié)果如下:
G:\Anaconda\python.exe "C:/Users/bingo/Desktop/The crawler/學(xué)習(xí)/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
到此這篇關(guān)于Python—處理文件(mimetypes和chardet)的文章就介紹到這了,更多相關(guān)Python—處理文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 深入探索Python解碼神器Chardet自動(dòng)檢測文本編碼
- Python chardet庫識(shí)別編碼原理解析
- Python3 chardet模塊查看編碼格式的例子
- python中判斷文件編碼的chardet(實(shí)例講解)
- Python中動(dòng)態(tài)檢測編碼chardet的使用教程
- python判斷字符串編碼的簡單實(shí)現(xiàn)方法(使用chardet)
- Python使用chardet判斷字符編碼
- python使用chardet判斷字符串編碼的方法
- 使用python的chardet庫獲得文件編碼并修改編碼
- python基于chardet識(shí)別字符編碼的方法
相關(guān)文章
Django多數(shù)據(jù)庫聯(lián)用實(shí)現(xiàn)方法解析
這篇文章主要介紹了Django多數(shù)據(jù)庫聯(lián)用實(shí)現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Python?pandera數(shù)據(jù)驗(yàn)證和清洗的庫
為了確保數(shù)據(jù)的質(zhì)量,Python Pandera 庫應(yīng)運(yùn)而生。本文將深入介紹 Python Pandera,這是一個(gè)用于數(shù)據(jù)驗(yàn)證和清洗的庫,并提供豐富的示例代碼,幫助大家充分利用它來提高數(shù)據(jù)質(zhì)量2024-01-01
2025最新版Python3.13.1安裝使用指南(超詳細(xì))
Python編程語言自誕生以來,已經(jīng)成為全球最受歡迎的編程語言之一,它簡單易學(xué)易用,以標(biāo)準(zhǔn)庫和功能強(qiáng)大且廣泛外 掛的擴(kuò)展庫,為用戶提供包羅萬象、強(qiáng)大全面的功能,此次給大家介紹了2025年最新版Python 3.13.1安裝使用指南全面更新,需要的朋友可以參考下2025-03-03
Python利用matplotlib實(shí)現(xiàn)餅圖繪制
Pyplot作為Matplotlib的子庫,提供了和MATLAB差不多的繪圖API。因此Pyplot作為常用的繪圖模塊,能很方便讓用戶繪制2D圖表。本文將為大家介紹如何利用Matplotlib繪制餅圖,感興趣的小伙伴可以了解一下2021-12-12
Python中的time模塊與datetime模塊用法總結(jié)
Python中內(nèi)置的各項(xiàng)時(shí)間日期函數(shù)幾乎都來自于time和datetime這兩個(gè)模塊,下面整理了Python中的time模塊與datetime模塊用法總結(jié),需要的朋友可以參考下2016-06-06

