我是一个有用的助手,可以为您翻译文本。
我有一个文件夹,里面包含5个文本文件,与各种站点相关--标题以这种方式格式化:
更新
http://codepad.org/3mQEM75e
从collections导入defaultdict import glob import csv
我有一个文件夹,里面包含5个文本文件,与各种站点相关--标题以这种方式格式化:
Rockspring_18_SW.417712.WRFc36.ET.2000-2050.txt
Rockspring_18_SW.417712.WRFc36.RAIN.2000-2050.txt
WICA.399347.WRFc36.ET.2000-2050.txt
WICA.399347.WRFc36.RAIN.2000-2050.txt
因此,文件名基本上遵循以下格式 - (网站名称)。 (网站编号)。 (WRFc36)。 (某些变量)。 (2000-2050.txt
每个文本文件都具有相似的格式,没有标题行:年 月 日 值(每个文本文件中包含约18500行)
我希望Python搜索类似的文件名(其中网站名称和网站编号匹配),并从其中一个文件中选择第一到第三列数据,并将其粘贴到新的txt文件中。 我还想复制并粘贴每个变量的第4列数据(雨,et等)并按特定顺序将它们粘贴到新文件中。
我知道如何使用csv模块(并定义用于空格分隔符的新方言)从所有文件中抓取数据并打印到新文本文件中,但我不确定如何自动创建每个网站名称/编号的新文件,并确保我的变量按正确顺序绘制出来-
我想要使用以下格式的一个文本文件(而不是5个),用于每个站点的输出,格式为(年,月,日,变量1,变量2,变量3,变量4,变量5),包含约18500行...
我相信我在这里忽略了一些非常简单的东西...... 这似乎是相当基础的...... 但是,任何帮助都将不胜感激!更新
========
http://codepad.org/3mQEM75e
从collections导入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.
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()
glob.iglob('*.txt')
将创建一个迭代器并避免创建值列表。 - hochl