I find each time I read a line, the end of of line character is included as well (showing as '$' in vi set list command) and am wondering how to automatically remove it when reading from a file?
From the print output, you can see end of line is output each time so that it seems two lines are printed each time.
Here is the code and input file, with output,
import sys
fileInput = open(sys.argv[1], 'r')
for line in fileInput:
print line
python TestLineParse.py TestInput.txt.csv
216
218
219
248
head TestInput.txt.csv
216
218
219
248
Try using the rstrip function to remove the newline character:
import sys
fileInput=open(sys.argv[1],'r')
for line in fileInput:
print line.rstrip('\n')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With