Problem: In Windows systems, the newline symbol is ' \ r \ n '. Python reads the file in order to be compatible with the system, will default to ' \ R ', ' n ', ' \ r \ Nand ' are treated as newline. However, in Windows files, it is possible to have ' \ n ', ' \ n ', ' \ R ' on the same line. This time the default behavior of Python splits a row into multiple lines of output, affecting the expected result.
You will need to set the newline parameter of the open function to modify the default behavior of the Python swap line.
Open (file, mode= ' R ', Buffering=-1, Encoding=none, Errors=none, Newline=none, Closefd=true, Opener=none)
NewLine has five kinds of values: None, ', ' \ n ', ' \ R ', ' \ r \ n '.
In the input process (from file to program), newline is used to define a newline symbol:
1. If newline is none, encounter ' \ R ', ' \ n ', ' \ r \ n ' are counted as the end of the line, and these symbols will be converted to ' \ n '.
2. If newline is ', it also encounters ' \ R ', ' \ n ', ' \ r \ n ' are counted as the end of the line, but these symbols will not be converted.
3. If the newline is ' \ r ', ' \ n ', ' \ r \ n ', the display specifies a newline character, and the symbol in the row does not change.
In the output process (from program to file), newline is used to specify the conversion symbol for ' \ n ':
1. If newline is none, all of the ' \ n ' are converted to a system line break.
2. If newline is ', ' \ n ', the conversion will not occur.
3. If newline is ' \ r ', ' \ r \ n ', all ' \ n ' will be converted to ' \ r ' or ' \ r \ n '.
Example One: output does not specify newline, all ' \ n ' are replaced with ' \ r \ n ', even if ' \ n ' is not the exception.
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 ()
Output Result:
[' I am a\r good\r\n ', ' boy.\r\r\n ']
Example Two: output specified newline ' or ' \ n ', does not convert
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 C6/>with Open ("Medical.txt", "R", newline= "\ 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 ()
Output Result:
[' I am a\r good\n boy.\r\n '] [' I am a\r good\n boy.\r\n ']
example Three: output specified newline ' \ r ' or ' \ r \ n ', all of ' \ n ' are replaced, when all ' \ n ' is replaced with ' \ R ', in Windows, the line break is gone, all rows become a row
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 ()
Output Result:
["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 ']
Example four: the input does not specify newline, by default all three symbols are treated as line breaks, and all are converted to ' \ 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 ()
Output Result:
[' I am a\n ', ' good\n ', ' boy.\n ']
Example Five: input specified newline ", still three symbols as newline characters, but do not convert
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 ()
Output Result:
[' I am a\r ', ' good\n ', ' boy.\r\n ']
Example Six: enter the specified newline as ' \ R ', ' \ n ', ' \ r \ n ', explicitly specify a newline character, only the few symbols will be wrapped.
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 ", N Ewline= "") 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 sto p\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 Chan GE the line \ r \ n ") f.write (" I can ' t stop\r\n ") #input with open (" Medical.txt "," R ", newline=" \ r ") as f:< C13/>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 ()
Output Result:
[' 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 ']
Conclusion:
1. If you want to write a line with ' \ n ', you can set the newline to ' or ' \ n ' to avoid python changes ' \ n '
2. If you want to read into the line with ' \ n ', you can set the newline to ' \ r \ n ' and specify that the newline character can only be ' \ r \ n '.
Python tips: File reads-the problem of line breaks