Convert CRLF to LF
def convertCRLFtoLF(self, filename):
WINDOWS_LINE_ENDING = b'\r\n'
UNIX_LINE_ENDING = b'\n'
with open(filename, "rb") as f:
content = f.read()
with open(filename, "wb") as f:
content = content.replace(WINDOWS_LINE_ENDING, UNIX_LINE_ENDING)
f.write(content)
用 rb wb 而不是 r w
如果用 r w 開啟 Text Mode,不管你是 windows 還是 unix ,換行都會被自動轉成 \n。所以用 str.replace() 取代不了,因為找不到 \r\n。用 binary string
如果沒有特別宣告,python3 會用 utf-8 去讀取操作,所以前加個 b 。參考資料 : stackoverflow/how-to-convert-crlf-to-lf-on-a-windows-machine-in-python
0 意見:
張貼留言