gpt4 book ai didi

Python解析文本文件的选定区域/范围

转载 作者:太空宇宙 更新时间:2023-11-03 18:17:52 27 4
gpt4 key购买 nike

我想打印文本文件 text.txt 的选定部分,其中包含:

tickme 1.1(no.3)
lesson1-bases
lesson2-advancedfurther
para:using the dsp signals are sened via a transmitter to conver to analong signals and gets the output in the form of digital signal

tickme 2.2(no.4)
lesson1-basics2
lesson2-advanced4
para:using the DIGITAL signals are sened via a transmitter to conver to analong signals and gets the output in the form of digital signal

tickme 3.3(no.5)
lesson1-bas
lesson2-adv
para:using the dsp signals are sened via a transmitter to conver to analong signals and gets the output in the form of digital signal

这里它由 3 个勾号组成,但我想选择一个特定的勾号 2.2(no.4),我必须在勾号 3.3(no.5) 开始之前解析文本。

所以,如果“DIGITAL”在段落中,它必须打印“lesson1”,因为它会导致歧义,我想选择文本文件的范围。

输出:

lesson1-basics2

由于它有所有tick me通用的第1课,我需要选择特定区域来解析tickme2.2(no.4)到tickme 3.3(no.5)有没有办法获取特定区域的范围并进行解析?并且可能会有大量的数据在刻度之间与示例不同。

问题:它没有选择特定范围。

searchstring = ""
with open('text.txt','r') as file:

for line in file:
if 'tickme' in line:
searchstring = line
if searchstring != '':
if 'DIGITAL' in line:
print(searchstring)
print(line)

最佳答案

将文件分成多个批处理,如下所示:

with open('txt.txt') as f: 批处理 = []

t = []
for l in line:
if 'tickme' in l:
# Start a new bactch after saving the previous batch
if t: batches.apend(t)
t = [l.strip()]
else:
if len(l.strip()) > 1: t.append(l.strip())

关于Python解析文本文件的选定区域/范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24704172/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com