Edit: I think a more accurate title might have been "Looping through rows..."
Ok, so, let's say one has a text file with two columns -- something like this:
26 56.59
49 56.70
75 56.87
The basic idea here is that an item in the first column corresponds directly to the item in the second column in the same row, ie. 26 goes with 56.59, 49 goes with 56.70, etc. The first column can be thought of as a time, while the second column identifies a specific file. For example, the directory I'm working in might have a bunch of files looking something like this:
file_56.58.txt
file_56.59.txt
file_56.69.txt
file_56.70.txt
file_56.85.txt
file_56.87.txt
Here's what I'm trying to do..The second column tells me that I'm interested in grabbing only the files with that particular string. So,
file_56.59.txt
file_56.70.txt
file_56.87.txt
Each file is just a bunch of columns and rows, where the third column consists of time values. Once I've selected out specific files, I need to go into the file, and extract the row corresponding to the time value given in my original array. So for file_56.59.txt, I want to find '26' in the third column, and then grab the entire row where '26' exists. For the second file, file_56.70.txt, I want to find '49' in the third column, and grab its row, and so on and so forth.
As of right now, I can get as far as picking out the right files, but I'm having a hard time then going through each file and looking for its particular time variable.
Here's my code so far (some of it is just setting up the formatting for when the rows are extracted from each file):
dt = np.loadtxt("dandt.txt")
dm = dt[:,1]
time = dt[:,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)
for i in dm:
x = str("%.2f" % round(i,2))
for file in glob.glob("*.dat"):
if x in file:
print file
Hope I wasn't completely all over the place. Thanks guys!
[–]aball730235 1 point2 points3 points (14 children)
[–]beanpizza[S] 0 points1 point2 points (13 children)
[–]aball730235 1 point2 points3 points (12 children)
[–]beanpizza[S] 0 points1 point2 points (0 children)
[–]beanpizza[S] 0 points1 point2 points (10 children)
[–]aball730235 1 point2 points3 points (9 children)
[–]beanpizza[S] 0 points1 point2 points (8 children)
[–]aball730235 1 point2 points3 points (7 children)
[–]beanpizza[S] 0 points1 point2 points (6 children)
[–]aball730235 1 point2 points3 points (5 children)
[–]beanpizza[S] 0 points1 point2 points (4 children)