python tips:檔案讀取——分行符號的問題

來源:互聯網
上載者:User

標籤:參數   can   符號   設定   enc   erro   The   pre   tips   

問題:在windows系統中,換行的符號是‘\r\n‘。python在讀檔案的時候為了系統相容,會預設把‘\r‘,‘n‘,‘\r\n‘都視作換行。但是在windows檔案中,可能在同一行中同時存在‘\n‘,‘\r\n‘,‘\r‘。這個時候python的預設行為會將一行拆分成多行輸出,影響預期結果。

此時需要設定open函數的newline參數,修改python對換行的預設行為。

open(file, mode=‘r‘, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

newline有五種取值:None,‘‘,‘\n‘,‘\r‘,‘\r\n‘。

在輸入過程(從檔案到程式),newline用於定義換行的符號:

1.如果newline為None,碰到‘\r‘,‘\n‘,‘\r\n‘都算行尾,而且這些符號都會被轉換成‘\n‘。

2.如果newline為‘‘,也是碰到‘\r‘,‘\n‘,‘\r\n‘都算行尾,但是這些符號不會發生轉換。

3.如果newline為‘\r‘,‘\n‘,‘\r\n‘,等於是顯示指定了分行符號,而且行中的符號不會發生轉換。

在輸出過程(從程式到檔案),newline用於指定‘\n‘的轉換符號:

1.如果newline為None,所有的‘\n‘都被轉換成系統分行符號。

2.如果newline為‘‘,‘\n‘,不會發生轉換。

3.如果newline為‘\r‘,‘\r\n‘,所有的‘\n‘會被轉換成‘\r‘或者‘\r\n‘。

執行個體一:輸出不指定newline,所有的‘\n‘都被替換成‘\r\n‘,即使是‘\r\n‘中的‘\n‘也不例外。

def file_seperator_test1():    # output    with open("medical.txt", "w") as f:        f.write("I am a\r good\n boy.\r\n")    #input    with open("medical.txt", "r", newline="\r\n") as f:        print(list(f))if __name__ == "__main__":    file_seperator_test1()

輸出結果:

[‘I am a\r good\r\n‘, ‘ boy.\r\r\n‘]

執行個體二: 輸出指定newline為‘‘或‘\n‘,不會轉換

def file_seperator_test2():    # output    with open("medical.txt", "w", newline="") as f:        f.write("I am a\r good\n boy.\r\n")    with open("medical2.txt", "w", newline="\n") as f:        f.write("I am a\r good\n boy.\r\n")    #input    with open("medical.txt", "r", newline="\r\n") as f:        print(list(f))    with open("medical2.txt", "r", newline="\r\n") as f:        print(list(f))if __name__ == "__main__":    file_seperator_test2()

輸出結果:

[‘I am a\r good\n boy.\r\n‘][‘I am a\r good\n boy.\r\n‘]

執行個體三:輸出指定newline為‘\r‘或‘\r\n‘,所有的‘\n‘都被替換了,當所有‘\n‘都被替換成‘\r‘時,在windows中,分行符號就不見了,所有的行變成了一行

def file_seperator_test3():    # output    with open("medical.txt", "w", newline="\r") as f:        f.write("I am a\r good\n boy.\r\n where should\r\n I change the line ?\r\n")        f.write("I can‘t stop\r\n")    with open("medical2.txt", "w", newline="\r\n") as f:        f.write("I am a\r good\n boy.\r\n")    #input    with open("medical.txt", "r", newline="\r\n") as f:        print(list(f))    with open("medical2.txt", "r", newline="\r\n") as f:        print(list(f))if __name__ == "__main__":    file_seperator_test3() 

輸出結果:

["I am a\r good\r boy.\r\r where should\r\r I change the line ?\r\rI can‘t stop\r\r"][‘I am a\r good\r\n‘, ‘ boy.\r\r\n‘]

執行個體四:輸入不指定newline,預設把所有的三種符號都當做分行符號,而且全都轉換成‘\n‘

def file_seperator_test4():    # output    with open("medical.txt", "w", newline="") as f:        f.write("I am a\r good\n boy.\r\n")    #input    with open("medical.txt", "r") as f:        print(list(f))if __name__ == "__main__":    file_seperator_test4() 

輸出結果:

[‘I am a\n‘, ‘ good\n‘, ‘ boy.\n‘]

執行個體五:輸入指定newline為‘‘,仍然把三種符號都當做分行符號,但是不轉換

def file_seperator_test5():    # output    with open("medical.txt", "w", newline="") as f:        f.write("I am a\r good\n boy.\r\n")    #input    with open("medical.txt", "r", newline="") as f:        print(list(f))if __name__ == "__main__":    file_seperator_test5()

輸出結果:

[‘I am a\r‘, ‘ good\n‘, ‘ boy.\r\n‘]

執行個體六:輸入指定newline為‘\r‘,‘\n‘,‘\r\n‘,顯式指定了分行符號,只有碰到這幾個符號才會換行

def file_seperator_test6():    # output    with open("medical.txt", "w", newline="") as f:        f.write("I am a\r good\n boy.\r\n where should\r\n I change the line ?\r\n")        f.write("I can‘t stop\r\n")    with open("medical2.txt", "w", newline="") as f:        f.write("I am a\r good\n boy.\r\n where should\r\n I change the line ?\r\n")        f.write("I can‘t stop\r\n")    with open("medical3.txt", "w", newline="") as f:        f.write("I am a\r good\n boy.\r\n where should\r\n I change the line ?\r\n")        f.write("I can‘t stop\r\n")    #input    with open("medical.txt", "r", newline="\r") as f:        print(list(f))    with open("medical2.txt", "r", newline="\n") as f:        print(list(f))    with open("medical3.txt", "r", newline="\r\n") as f:        print(list(f))if __name__ == "__main__":    file_seperator_test6()

輸出結果:

[‘I am a\r‘, ‘ good\n boy.\r‘, ‘\n where should\r‘, ‘\n I change the line ?\r‘, "\nI can‘t stop\r", ‘\n‘][‘I am a\r good\n‘, ‘ boy.\r\n‘, ‘ where should\r\n‘, ‘ I change the line ?\r\n‘, "I can‘t stop\r\n"][‘I am a\r good\n boy.\r\n‘, ‘ where should\r\n‘, ‘ I change the line ?\r\n‘, "I can‘t stop\r\n"]

結論:

1.如果要寫入帶‘\n‘的行,可以把newline設定為‘‘或者‘\n‘,避免python更改‘\n‘

2.如果要讀入帶‘\n‘的行,可以把newline設定為‘\r\n‘,指定分行符號只能是‘\r\n‘。

 

python tips:檔案讀取——分行符號的問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.