Monday, 15 February 2010

Python 3.x read text file without ignoring blank line at the end -


I tried to read a text file that looks like this:

 line 1 row 3 

/ Pre>

Use the following code:

Open with text ('textfile.txt', 'r') for f_in: f_in in line: print (" "+ Line) F_in.closed

But instead of Loop 4, it runs only 3 times, ignoring the empty line.

The output I get is:

  line 1 ~~ line 3  

How can I fix this?

When using a file as an iterator, end of a line \ n . This means that even if your editor can show "blank line" after 3 , if there is no data, then there is no line. For example, try this: ('textfile.txt', 'wb') with the following: ('Textfile.txt'):

  f_out: f_out.write (b ' 1 \ n \ n3 \ n4 ') as in f_in: f_in for the line: print ("~" + line)  

that last Python recognizes one more line, by entering the character on the line, 4 , even without a finished lineif Without that 4 , you only have three rows.


No comments:

Post a Comment