我只需要将第二个CSV文件的列添加到第一个CSV文件中。在
示例CSV文件#1
Time Press RH Dewpt Alt
Value Value Value Value Value
N行数。在
示例CSV文件#2
SmoothedTemperature
Value
我只想成功
Time Press RH Dewpt Alt SmoothedTemperature
Value Value Value Value Value Value
其中一个有标题,另一个没有。在
这里是我目前为止的示例代码,但是输出是重复的文件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)
如果你想走这条路,
pandas
使csv操作变得非常简单。假设前两个示例表位于名为test1.csv
和test2.csv
的文件中:这个新表可以用DataFrame方法
to_csv
保存到.csv
文件中。在如您所述,如果其中一个文件没有头,则可以在读取文件时指定:
^{pr2}$然后在
pandas
中手动更改标题行。在相关问题 更多 >
编程相关推荐