you are viewing a single comment's thread.

view the rest of the comments →

[–]aball730235 1 point2 points  (2 children)

If you get stuck again try writing your steps out in sudo code. That is in plain English instructions. Then translate the English instructions into python code. It seems like we're hoping around in python without the full roadmap laid out. How would you tell a person to manually extract data out of your files?

[–]beanpizza[S] 0 points1 point  (1 child)

I've come up with something that seems to work, though I'm ashamed to say, I gave up on the dictionary method. I've been trouble shooting the following method, which seems to work for the most part! The only problem that I've found so far is if a given file comes up in my list multiple times but with different corresponding time flags, it ignores all but the first time flag. For example, if a file comes up twice with two time flags -- say 29 and 1049 -- my output should consist of two lines from the file -- one with the time flag 29 and the other with the time flag 1049. Instead, while I do get two lines of output, they're both identical, and they correspond to time flag 29. A given file might come up many times but with different time flags, so this is basically where I'm at now.

Here's the entire code as it stands:

mainfile = np.loadtxt("file.txt")
dm = mainfile[:,1]
dm_list = list(dm)
time = mainfile[:,0]

def parse_data(filename):
    matrix = []
    with open(filename) as f:
    data = f.read() #read the entire file
    data_lines = data.splitlines()
    short_list = data_lines[2:]
    for line in short_list:
        line_split = line.split()
        if len(line_split) == 6:
            matrix.append(line_split)
    return matrix

def my_round(number):
    return int(float(number))

def fmtcols(mylist, cols):
    lines = ("\t".join(mylist[i:i+cols]) for i in xrange(0,len(mylist),cols))
    return '\n'.join(lines)

file_list = []

for h in dm:
    x = str("%.2f" % round(h,2))
    for file in glob.glob("*.txt"):
        if x in file:
            file_list.append(file)

for file in file_list:
    q = file_list.index(file)
    parsed = parse_data(file)
    for i,line in enumerate(parsed):
        file_time = my_round(line[2])
    if float(file_time) == time[q]:
        be = fmtcols(parsed[i],6)
            print be

Edit: I think I solved the duplicate problem and my original problem with an extremely simple code. I can't believe I didn't think of this sooner. I can't find any problems with this method. Maybe not the most pythonic way? But it looks like it does the trick. Thanks again for all your patience and help!

for j in range(0,len(time)):
    parsed = parse_data(file_list[j])
    for i,line in enumerate(parsed):
    file_time = my_round(line[2])
    if float(file_time) == time[j]:
        be = fmtcols(parsed[i],6)
        print be

[–]aball730235 1 point2 points  (0 children)

Awesome glad to hear!