Reputation: 4060
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.
Upvotes: 3
Views: 7393
Reputation: 715
using list comprehension, you can do this as below:
def readfile():
with open(filename, 'r') as fh:
lines = [line.rstrip('\n') for line in fh.readlines()]
for line in lines:
print (line)
Upvotes: 0
Reputation: 27822
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')
.
Upvotes: 7
Reputation: 414675
The 144th character is a newline character.
with open("file_path") as file:
for line in file:
line = line.rstrip("\n") # strip newline
print(len(line)) # should print 143
Upvotes: 0