Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can try this code, the main idea is to read the files iteratively, and update a dictionary object with the count and sum of the second and third values in each number, good luck!</p> <pre><code>#first you get the paths for all the dat files: import os dat_dir=r'c:\dat_dir' our_files=[path for os.path.join(dat_dir,f) for f in os.listdir(dat_dir)] #then you iterate over them and update a dictionary object with the results for each file: dict_x_values={} for f in our_files: fopen=open(f,'r') for line in fopen: line=line.strip('\n') split=[int(v) for v in line.split()] if len(split)==3: key=split[0] if dict_x_values.has_key(key): second_count,second_sum=dict_x_values[key][0] #the second value in the row second_count+=1 #we increment the count second_sum+=split[1] #we increment the sum third_count,third_sum=dict_x_values[key][1] #the third value in the row third_count+=1 third_sum+=split[2] dict_x_values[key]=[[second_count,second_sum],[third_count,third_sum]] else: dict_x_values[key]=[[1,split[1]],[1,split[1]]] #if the dictionary doesn't have the left x-value, we initialize it fopen.close() #Then we write our output combined file comb_open=open('comb_dat.txt','w') for key in dict_x_values: second_count,second_sum=dict_x_values[key][0] #the second value in the row third_count,third_sum=dict_x_values[key][1] #the third value in the row second_avg=float(second_sum)/second_count third_avg=float(third_sum)/third_count line='%s\t%s\t%s'%(key,second_avg,third_avg) comb_open.close() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload