Skip to content Skip to sidebar Skip to footer

Finding A Certain Block Of Code In Text

I have the following code so far: import sys from Tkinter import * import tkFileDialog from tkFileDialog import askopenfile # Open dialog box fen1 = Tk()

Solution 1:

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

defconvert_type(ss):
    try:
        returnint(ss)
    except ValueError:
        try:
            returnfloat(ss)
        except ValueError:
            return ss

defparse_file(ff):
    out={}
    block=Nonefor i,line inenumerate(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 isnotNone) 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()))
                raisereturn out

withopen('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.

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]

Solution 2:

Perhaps something like this:

line = filename.readline()
if line.find("*load") == 0:
    line = filename.readline()
    while line != "\n"and line != "":
        vars = line.split(" ")

vars is just an example to store the values which would be ['2', '7', '200'] after this code runs (so you would need to convert them to floats or ints). You could then append these to an array or rename them as needed.

EDIT: Working program derived from the above.

filename = open("fff.txt", 'r')
values = {}

line = filename.readline()
while line:
    while line.find("*") != 0:
        line = filename.readline()

    sectionheader = line.strip()[1:]
    values[sectionheader] = []
    line = filename.readline()
    while line != "\n"and line != "":
        vals = [float(i) for i in line.split(" ")]
        values[sectionheader].append(vals)
        line = filename.readline()

printvalues

Solution 3:

Though I cannot help you with the syntax, it is probably best to use self invokation.

Write a function that detects the line you need and store the byte offset. Next, make that function invokate itself to find the next line ( to end the operation ), store its offset also and compare it with the previously saved value.

Now you have enough data to pinpoint which bytes need to be changed.

Self invoking functions are however quite efficient when used right they speed up performance and are easy to reuse.

In php I have build a streamwriter similar to the one in .net which works this way. I therefore know the theory works, however, this seems to be python.

I don't know enough of that language unfortunately. Good luck with your project though!

Solution 4:

This is what I made of your code:

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)

defdo_open(interesting_parts=[]):
    thefile = askopenfile(filetypes=[("Text files","*.txt")], mode='r')

    data = {} # Create a dictionary to store all the data stored per part name
    part = Nonefor line in thefile:
        if line.startswith("*"):
            # A * in the beginning signals a new "part"# And another one means the end.
            part = line[1:] # Remove the * in the beginning via slicing to get the name without it.if part in interesting_parts:
                data[part] = [] # Create a list inside the dictionary, so that you can add data to it later.# Only do this if we are interested in storing anything for this partelif part in interesting_parts:
            # Add an a condition to check if the part name is in the list of "parts" you are interested in having.
            line_data = get_something_from_this(part, line) # just a function that returns something based on the line and the partif line_data isnotNone: # Ignore it if it's None (just an option, as there might be newlines that you want to ignore)
                data[part].append(line_data)

    # Here, we return the dictionary to act on it.return data

defget_something_from_this(part, line):
    try:
        ints = [int(p) for p in line.split()]
    except ValueError:
        print"Ignoring Line", repr(line), "in", part
        returnNone# We don't care about this line!else:
        print"in", part, ints
        return ints # Store this line's data

data = do_open(["test", "egg"]) # Pass as an argument a list of the interesting "parts"print data # this is a dictionary# How do you access it?print data["test"] # to get all the lines' data in a listprint data["egg"][0] # to get the first line of the datafor part, datalines in data.iterkeys():
    print part, datalines # datalines is the list of all the data, part is the dictionary's key which is the part name# Remember data[part] = ... <- Here part is the key.

fen1.mainloop()
  1. Don't name the variable filename when it's not a "file name" but a "file".
  2. You can use the for loop to loop through the lines one by one.
  3. Use split to split a string
  4. Use startswith to know if a string starts with another string
  5. Keep track of whether or not you're in the "*load" part in a variable.

UPDATE: Don't use open as a function name, it's already in python's builtins. Also, to avoid parsing the *load and *stiffness lines, I modified a bit the code: the parsing of each line is done in an elif statement.

UPDATE 2:

Updated code according to OP's needs. Tested with this file:

*test123*load2720037150*stiffness298*egg123246*plant234578

UPDATE 3: Heavily commented :)

Solution 5:

something like this should do

data=[]
check=falsefor i in fid:
    if line.find("*load"):
        check=trueif check==true and not line.find("*stiffness"):
        line=split(i)
        data.append(map(lambda x: float(x), line))
    if line.find("*stiffness"):
        break

fid.close()
for i indata:
    a=i[0]
    b=i[1]
    c=i[2]

take this as a code as rough suggestion ... (I think the exception is fixed now, well if not I don't care...)

Post a Comment for "Finding A Certain Block Of Code In Text"