python解析文本文件的选定部分

时间:2014-07-12 00:16:18

标签: python

我正在尝试一个程序从文本文件中打印文本的选定部分:text.txt

txt.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个勾选我,但我想选择一个特定的tickme 2.2(no.4),我必须在开始tickme 3.3(no.5)之前解析文本

所以,如果DIGITAL处于para状态,则必须打印leason1,因为它会引起歧义我想选择文本文件范围

输出:

lesson1-basics2

由于它有一个共同的第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

请帮忙!答案将不胜感激

2 个答案:

答案 0 :(得分:0)

您可以将范围指定为函数参数。然后,您可以将该范围内的所有行保存在列表中,并使用该行列表进行进一步分析。

#Function to accept file name, start line of range, end line of range
#Returns a list of lines within that range 
def select_range(filename, start_range, end_range):
f = open(filename, 'r')
selected_lines=[]
start=0
for line in f:
    if start_range in line:
        start=1
    if end_range in line:
        start=0
        return selected_lines
    if start==1:
        selected_lines.append(line)
return selected_lines

lines=select_range("tickme.txt","tickme 2.2(no.4)","tickme 3.3(no.5)") print lines

答案 1 :(得分:0)

将文件拆分为如下批次:

with open('txt.txt') as f:
    batches = []

    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())

然后事情变得容易了......