简体   繁体   English

在两个相同的字符串之间从python的txt文件中读取文本块

[英]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.

相关问题 使用python读取文本文件中两个字符串之间的行 - Reading lines between two strings in text file using python 如何对相同的字符串求和并从 python 中的 txt 文件中对它们进行排序 - How to sum identical strings and sort them from txt file in python Python,用于从两个指定字符串之间的文件中读取信息(当这些字符串可以出现在其他位置时) - Python for reading information from a file between two specified strings when these strings can be present elsewhere python3提取txt文件中两个字符串之间的字符串 - python3 extract string between two strings in a txt file 使用 python 从另一个文本文件中的两个字符串之间替换文件中的文本 - Replace text in a file between two strings from another text file using python 使用 Python 提取文本文件中两个字符串之间的文本 - Extract text present in between two strings in a text file using Python 从文件读取两个字符串 - Reading two strings from file 在python中使用正则表达式获取txt文件中的文本块 - Get block of text in a txt file with regex in Python 使用BeautifulSoup和Python从网页中提取两个文本字符串之间的文本 - Extract text between two text strings from webpage with BeautifulSoup and Python 从.txt文件读取时如何检查两个字符串是否在同一行上 - How to check if two strings are on the same line when reading from a .txt file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM