Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove end of line in Python? [duplicate]

Tags:

python

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
like image 321
Lin Ma Avatar asked Oct 29 '25 11:10

Lin Ma


1 Answers

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')
like image 136
Tim Biegeleisen Avatar answered Nov 01 '25 02:11

Tim Biegeleisen



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!