Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: file.readline adding a space at the end of the line

Tags:

python

Am using python to read a flat space-padded text file. Part of the validation of the text file is that each line in the text file is expected to be a specific file length including the space padding.

When I use the following code, python ends up giving me a line with an extra space. E.g. I expect all the rows in fileX to have 143 characters. Python though will read this as 144 characters and thus say the file is invalid. If I do the same in VB.NET, I get the correct 143 characters.

Why is Python's readline function adding an extra character? (Using python 3.2)

import io
myfile = open("file_path", "r")
while True:
    line = myfile.readline()
    if not line:
            break
    print(len(line))  #This prints 144 characters

VB.NET gives the correct length of 143 characters.

Using objStreamReader As StreamReader = New StreamReader(myFilePath)
    While objStreamReader.EndOfStream = False
            line = objStreamReader.ReadLine
            len(line)   'This returns the correct length of 143.

Using line.strip will not be the right mechanism because I might get rid of useful spaces. Remember the file is space-padded up to a maximum given length.

like image 917
lukik Avatar asked Dec 02 '25 02:12

lukik


1 Answers

objStreamReader.ReadLine chops off the terminating newline, whereas Python's file.readline keeps it.

If your file was opened in text mode (and unless you explicitly specified otherwise, it was), the line ending will always be either nothing (last line only) or exactly one \n, and you can safely chop it off with rstrip('\n').

like image 137
Cairnarvon Avatar answered Dec 04 '25 16:12

Cairnarvon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!