Python codecs.open 函式
Vaibhhav Khetarpal
2022年5月17日

本教程討論 Python 中的 codecs.open()
函式。
codecs.open()
函式與 Python 中內建的 open()
函式並行工作,並開啟具有特定編碼的檔案。預設情況下,它以讀取模式開啟檔案。
codecs.open()
函式以二進位制模式開啟所有檔案,即使程式碼語法中沒有手動提及。這避免了在處理 8 位編碼時可能發生的資料丟失。
codecs.open()
函式的語法如下:
codecs.open(filename, mode="r", encoding=None, errors="strict", buffering=-1)
上述函式語法中的引數包含它們的預設值。
在 Python 2.6 版釋出後,codecs.open()
函式已過時。Python 新增了另一個 io.open()
函式,用於增強內建 open()
函式的功能。
io.open()
函式的語法主要與 codecs.open()
函式進行比較,它與 codecs.open()
函式相對不同,具體如下。
io.open(
file,
mode="r",
buffering=-1,
encoding=None,
errors=None,
newline=None,
closefd=True,
opener=None,
)
codecs.open()
函式雖然仍然存在於較新的版本中,但沒有實際價值,主要用於向後相容。
Vaibhhav is an IT professional who has a strong-hold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.
LinkedIn