我有多个.csv
文件到一个文件夹中,并且它们具有相同的结构。
前两列包含两个值k1
和k2
,第三列包含一个数值。
我想遍历这些文件并将它们合并到一个新的csv
中,在其中我将有K1
和K2
的前两列,然后是n
列,每个n
文件。
这是我的解决方案:
import glob
import csv
import os
def get_data(filename):
'''function to read the data form the input csv file to use in the analysis'''
with open(filename, 'r') as f:
reader = csv.reader(f)
result = {tuple(row[:2]): row[2] for row in reader}
return result
path='mypath'
for infile in glob.glob(os.path.join(path, '*.csv')):
print ("Current File Being Processed is: " + infile)
#use split to seperate the path and name of the file
(PATH, FILENAME) = os.path.split(infile)
all_data=[]
#adds the data from the csv file to a blank list so it can be operated on
all_data.extend(get_data(infile))
result = {}
for dic in all_data:
for key in (dic.viewkeys() | result.keys()):
if key in dic: result.setdefault(key, []).append(dic[key])
else: result.setdefault(key, []).append(0)
with open('mypath', 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in result:
writer.writerow(data)
但返回
Traceback (most recent call last):
File "C:\Users\Desktop\p.py", line 26, in <module>
for key in (dic.viewkeys() | result.keys()):
AttributeError: 'tuple' object has no attribute 'viewkeys'
答案 0 :(得分:1)
result = {tuple(row[:2]): row[2] for row in reader}
您将键存储为元组(键),因此对其进行迭代会为您抛出错误。