在文本中查找某个代码块 [英] Finding a certain block of code in text

查看:68
本文介绍了在文本中查找某个代码块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我有以下代码:

I have the following code so far:

import sys
from Tkinter import *
import tkFileDialog
from tkFileDialog import askopenfile # Open dialog box


fen1 = Tk()                              # Create window
fen1.title("Optimisation")               # Window title

menu1 = Menu(fen1)

def open():
    filename = askopenfile(filetypes=[("Text files","*.txt")], mode='r')

filename.seek(0)
    numligne = 0
    line     = []
    ok       = 0
    k        = -1

    while (ok == 0)  &  (k == -1):
    line = filename.readline()
    k    = line.find( "*load" )
    if k == 0 :
        l = filename.readlines()

fen1.mainloop()

我搜索的文本文件格式类似于以下:

The text file I am searching is in the format similar to below:

*test
1 2 3 4

*load
2 7 200
3 7 150

*stiffness
2 9 8 7

etc..

到目前为止,我已经设法找到以*load"开头的行,但我希望将*load"和*stiffness"之间的值分配给诸如 a、b、c 之类的变量.我的问题是在这个加载部分,可能有几行,我每次都需要检测每一行,分割行中的值并给它们一个名字.如果有人可以帮助解释一个循环或类似的东西,可以做到这一点,我将不胜感激!谢谢!

I've so far managed to find the line beginning with "*load" but I wish to assign the values in between '*load' and '*stiffness' to variables such as a, b, c. My problem is that in this load section, there could be several lines and I need to detect each one every time, split the values in the lines and give them a name. If someone could please help explain a loop or something similar that would do just the trick, I would be very grateful! Thank you!

更新:我有一个问题,我现在想在同一个文本文件中找到几个单独的部分.我怎样才能创建一个循环来进一步查找 '*geo' 和 '*house' 以及 '*name' 和 '*surname' 之间的行?我试图创建一个完全独立的定义,但希望尽量减少我使用的代码行数……谢谢!代码我一直在使用类似的结构(为我的原始问题提供,感谢 mgilson!),因此想要编辑这些类型的代码.

UPDATE: I have the problem where I now want to find SEVERAL seperate sections in the same text file. How am I able to create a loop to further find lines between '*geo' and '*house', and also '*name' and '*surname'? I've tried to create an entirely separate definition but would like to minimize the lines of code I use...Thank you! Code I've been using the similar structure for (as provided for my original question, thanks to mgilson!) and would therefore like to edit these type of code.

def parse_file(ff):     
    out=[]     
    append=False     
    for line in ff:         
        if(append and line.strip()):
            out.append(line)          
            if(line.startswith('*load')): 
                append=True
            elif(line.startswith('*stiffness')):  
                return [map(int,x.split()) for x in out[:-1] ] 

推荐答案

让我们假设您的代码块"由标题分隔(例如 *header).将数据存储在每个块中的最直观方式是在列表列表中.例如[ row1, row2, ...] (其中 row1=[elem1,elem2,elem3,...]).然后,您可以将该块存储在字典中,以便您可以通过 block=dictionary['headername'] 访问该块.

Let's assume that your "blocks" of code are separated by headers (e.g. *header). The most intuitive way to store the data in each block is in a list of lists. e.g. [ row1, row2, ...] (where row1=[elem1,elem2,elem3,...]). Then you can store the block in a dictionary so you can get access to the block via block=dictionary['headername'].

这将执行您想要的操作(此版本未经测试).

This will do something like what you want (this version is untested).

import sys

def convert_type(ss):
    try:
        return int(ss)
    except ValueError:
        try:
            return float(ss)
        except ValueError:
            return ss

def parse_file(ff):
    out={}
    block=None
    for i,line in enumerate(ff):
        #Allow for comments to start with '#'.  We break off anything after a '#'
        #and ignore it.  After that, we 
        data=line.split('#',1)
        line=data[0]  #comments (if in line) are in data[1] ... ignore those.
        line=line.strip() #remove whitespace from front and back of line.
        if(line.startswith('*')):
            #python supports multiple assignment.  
            #e.g. out['header'] is the same object as block.  
            #     changing block also changes out['header']
            block=out[line.strip()[1:]]=[]
        elif (block is not None) and line: #checks to make sure there is an active block and the line wasn't empty.
            #If the file could also have floats, you should use float instead of int
            #We also put the parsing in a try/except block.  If parsing fails (e.g. a
            #element can't be converted to a float, you'll know it and you'll know the
            #line which caused the problem.)
            try:
                #block.append(map(int,line.split()))
                block.append(map(convert_type,line.split()))  
            except Exception:
                sys.stderr.write("Parsing datafile choked on line %d '%s'\n"%(i+1,line.rstrip()))
                raise
    return out

with open('textfile.txt','r') as f:
    data_dict=parse_file(f)

#get information from '*load' block:
info=data_dict['load']
for row in info:
    a,b,c=row
    ##same as:
    #a=row[0]
    #b=row[1]
    #c=row[2]
    ##as long as row only has 3 elements.

    #Do something with that particular row. 
    #(each row in the 'load' block will be visited once in this loop)

#get info from stiffness block:
info=data_dict['stiffness']
for row in info:
    pass #Do something with this particular row.

请注意,如果您保证某个标题下的数据文件中的每一行都具有相同数量的条目,您可以将变量 info 视为一个被索引的二维行作为 element=info[row_number][column_number] -- 但你也可以通过 row=info[row_number]

Note that if you're guaranteed that each row in the datafile under a certain header has the same number of entries, you can think of the variable info as a 2-dimensional row which is indexed as element=info[row_number][column_number] -- but you can also get an entire row by row=info[row_number]

这篇关于在文本中查找某个代码块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆