python - readlines gives me additional linebreaks python2.6.5 -
i have problems following code:
file = open("file.txt", "r") lines = file.readlines() print lines[0] print lines[1] print lines[2] file.close()
this code gives me linebreaks between lines. output this:
line0 line1 line2
how can solved?
print
adds newline. strip newline line:
print lines[0].rstrip('\n') print lines[1].rstrip('\n') print lines[2].rstrip('\n')
if reading whole file list anyway, alternative use str.splitlines()
:
lines = file.read().splitlines()
which default removes newlines lines @ same time.
Comments
Post a Comment