self.viewerData = []
tempViewerData = []
tempViewer = []
started = False
with open("tittardata.txt", "r") as fp:
for i, line in enumerate(fp.readlines()):
if line.startswith("=="):
started = True
continue
if started and line.startswith("--"):
started = False
if started == True:
tempViewerData.append(line.rstrip("\n"))
我正在尝试从下面的 txt 文件中读取 block ,这些 block 在两端用“---”分隔。 在第一个 block 中,分隔由以“===”开头并以“--”结尾的不同符号处理。即将到来的 block 由相同的符号解析,这使得提取 block 更加困难。到目前为止,这是我的尝试,感谢所有帮助。
这是文本文件的摘录:
=================
19.37/2
19.52/2
21.07/1
21.22/1
21.37/1
-------
19.37/2
19.52/2
-------
最佳答案
blocks = []
block = []
for line in f:
if line[:3] in ('===', '---'):
# don't record empty blocks, avoids empty block at start
if block:
blocks.append(block)
block = []
else:
block.append(line.rstrip('\n'))
# needed if last block is not bounded by separator
if block:
blocks.append(block)
关于python - 在两个相同的字符串之间从 python 中的 txt 文件中读取一段文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20843225/