如何在Python中删除行尾?
我发现每次读取一行时,都会包含行尾字符(在vi set list命令中显示为“$”),我想知道如何在读取文件时自动删除它 从打印输出中,您可以看到每次都输出行尾,因此每次似乎打印两行 这是代码和输入文件,以及输出如何在Python中删除行尾?,python,Python,我发现每次读取一行时,都会包含行尾字符(在vi set list命令中显示为“$”),我想知道如何在读取文件时自动删除它 从打印输出中,您可以看到每次都输出行尾,因此每次似乎打印两行 这是代码和输入文件,以及输出 import sys fileInput = open(sys.argv[1], 'r') for line in fileInput: print line python TestLineParse.py TestInput.txt.csv 216 218
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
尝试使用
rstrip
功能删除换行符:
import sys
fileInput=open(sys.argv[1],'r')
for line in fileInput:
print line.rstrip('\n')
尝试使用以下方法:
print line.rstrip()