codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
from collections import defaultdict import glob import csv #Create dictionary of lists-- [A] = [Afilename1, Afilename2, Afilename3...] # [B] = [Bfilename1, Bfilename2, Bfilename3...] def get_site_files(): sites = defaultdict(list) #to start, I have a bunch of files in this format --- #"site name(unique)"."site num(unique)"."WRFc36"."Variable(5 for each site name)"."2000-2050" for fname in glob.glob("*.txt"): #split name at every instance of "." parts = fname.split(".") #check to make sure i only use the proper files-- having 6 parts to name and having WRFc36 as 3rd part if len(parts)==6 and parts[2]=='WRFc36': #Make sure site name is the full unique identifier, the first and second "parts" sites[parts[0]+"."+parts[1]].append(fname) return sites #hardcode the variables for method 2, below Var=["TAVE","RAIN","SMOIS_INST","ET","SFROFF"] def main(): for site_name, files in get_site_files().iteritems(): print "Working on *****"+site_name+"*****" ####Method 1- I'd like to not hardcode in my variables (as in method 2), so I can use this script in other applications. #### This method is not working right now. for filename in files: reader = csv.reader(open(filename, "rb")) WriteFile = csv.writer(open("XX_"+site_name+"_combined.txt","wb")) for row in reader: row = reader.next() ####Method 2 works (mostly), but skips a LOT of random lines of first file, and doesn't utilize the functionality built into my dictionary of lists... ## reader0 = csv.reader(open(site_name+".WRFc36."+Var[0]+".2000-2050.txt", "rb")) #I'd like to copy ALL columns from the first file ## reader1 = csv.reader(open(site_name+".WRFc36."+Var[1]+".2000-2050.txt", "rb")) # and just the fourth column from all the rest of the files ## reader2 = csv.reader(open(site_name+".WRFc36."+Var[2]+".2000-2050.txt", "rb")) # (the columns 1-3 are the same for all files) ## reader3 = csv.reader(open(site_name+".WRFc36."+Var[3]+".2000-2050.txt", "rb")) ## reader4 = csv.reader(open(site_name+".WRFc36."+Var[4]+".2000-2050.txt", "rb")) ## WriteFile = csv.writer(open("XX_"+site_name+"_COMBINED.txt", "wb")) #creates new command to write a text file ## ## for row in reader0: ## row = reader0.next() ## row1 = reader1.next() ## row2 = reader2.next() ## row3 = reader3.next() ## row4 = reader4.next() ## WriteFile.writerow(row + row1 + row2 + row3 + row4) ## print "***finished with site***" if __name__=="__main__": main()
Private
[
?
]
Run code
Submit