我在尝试从txt文件中提取数据时遇到问题。解决办法肯定很简单,但无论如何我想我需要你的一点帮助。 现在它打印成“123456”,但我想打印成“123456”,有什么想法吗
输入
asd
ear
hello
hello
hello
rea
ear
world
world
rea
zxczxc
代码
check = False
data = ''
start = 'ear'
end = 'rea'
with open('bear.txt', 'r') as rf:
lines = rf.readlines()
for i,x in enumerate(lines):
if start in x:
check = True
if check:
data += str(x)
if end in x:
check = False
print(data)
print(i)
输出
ear
hello
hello
hello
rea
5
ear
hello
hello
hello
rea
ear
world
world
rea
9
预期产量
ear
hello
hello
hello
rea
5
ear
world
world
rea
4
data
输出:
相关问题 更多 >
编程相关推荐