[英]Reading a block of text from a txt file in python between two identical strings
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"))
I am trying to read the blocks from the txt file below which are separated by "---" on both ends. 我正在尝试从下面的txt文件中读取由两端“ ---”分隔的块。 On the first block the separation is handled by different symbols starting with "===" and ending with "--".
在第一个块上,分隔由不同的符号处理,这些符号以“ ===”开头,以“-”结尾。 The upcoming blocks are parsed by the same symbol, making it more difficult to extract the blocks.
即将到来的块由相同的符号解析,这使得提取块变得更加困难。 This is my attempt this far, all help is appreciated.
到目前为止,这是我的尝试,感谢所有帮助。
Here is an extract from the text file: 以下是文本文件的摘录:
=================
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)
Use a generator that effectively makes lines consisting only of =
or -
blank, then group by data that isn't blank, eg: 使用可以有效地使仅由
=
或-
空白的生成器,然后按非空白的数据分组,例如:
from itertools import groupby
with open('your_file') as fin:
lines = (line.strip('-=\n') for line in fin)
blocks = [list(g) for k, g in groupby(lines, bool) if k]
# [['19.37/2', '19.52/2', '21.07/1', '21.22/1', '21.37/1'], ['19.37/2', '19.52/2']]
If you don't need the data all at once, then make blocks
a generator instead and loop over that.... 如果您不需要一次全部数据,那么可以使用
blocks
生成器并在其上循环...。
blocks = (list(g) for k, g in groupby(lines, bool) if k)
for block in blocks:
# do something
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.