Data form like this:
line1
text1
text2
line2
something text1
line3
Making an output:
line1 text1 text2
line2 something text1
line3
how could I make it to delete?
Data form like this:
line1
text1
text2
line2
something text1
line3
Making an output:
line1 text1 text2
line2 something text1
line3
how could I make it to delete?
可以这样删除空行:
with open('input') as fin, open('output', 'w') as fout:
non_blank = (line for line in fin if line.strip())
fout.writelines(non_blank)
也许这可以满足您的要求:
import sys
i = 2
for line in sys.stdin:
if line.startswith('line%d' % i):
i += 1
sys.stdout.write('\n')
if line == '\n':
sys.stdout.write('\n')
else:
sys.stdout.write(line.rstrip() + ' ')
sys.stdout.write('\n')
这将贯穿输入行并收集文本,直到找到一个输入行lineN
。这样的一行在输入中开始新的一行。此外,输入中的空白行作为输出中的空白行传递。
我认为您的目标是删除空白,对吗?如果是这样的话:
inputfile = open('example.txt', 'r')
outputfile = open('examplelines.txt', 'w')
for line in inputfile:
if line.strip('\n') != '':
outputfile.write(line)
inputfile.close()
outputfile.close()