【发布时间】:2014-10-06 00:13:18
【问题描述】:
我只需要将第二个 CSV 文件的列添加到第一个 CSV 文件中。
示例 CSV 文件 #1
Time Press RH Dewpt Alt
价值价值价值价值价值
对于 N 行。
示例 CSV 文件 #2
平滑温度
价值
我只是想成功
Time Press RH Dewpt Alt SmoothedTemperature
价值价值价值价值价值价值
还有一个有标题,另一个没有。
这是我目前所拥有的示例代码,但输出是文件 1 的最后一行,旁边是文件 #2 的完整数据集。
##specifying what they want to open
File = open(askopenfilename(), 'r')
##reading in the other file
Averaged = open('Moving_Average_Adjustment.csv','r')
##opening the new file via raw input to write to
filename = raw_input("Enter desired filename, EX: YYYYMMDD_SoundingNumber_Time.csv; must end in csv")
New_File = open(filename,'wb')
R = csv.reader(File, delimiter = ',')
## i feel the issue is here in my loop, i don't know how to print the first columns
## then also print the last column from the other CSV file on the end to make it mesh well
Write_New_File = csv.writer(New_File)
data = ["Time,Press,Dewpt,RH,Alt,AveragedTemp"]
Write_New_File.writerow(data)
for i, line in enumerate(R):
if i <=(header_count + MovingAvg/2):
continue
Time,Press,Temp,Dewpt,RH,Ucmp,Vcmp,spd,Dir,Wcmp,Lon,Lat,Ele,Azi,Alt,Qp,Qt,Qrh,Qu,Qv,QdZ=line
for i, line1 in enumerate(Averaged):
if i == 1:
continue
SmoothedTemperature = line1
Calculated_Data = [Time,Press,Dewpt,RH,Alt,SmoothedTemperature]
Write_New_File.writerow(Calculated_Data)
【问题讨论】:
-
对我有用的规则是“之前,把它放在屏幕上。如果你可以把想要的输出放在屏幕上,你可以把它放到一个文件中”