到目前为止,我已经编写了一个代码,可以读取csv文件并以嵌套字典的形式返回数据。 csv文件中表的第一列包含键,并且其键值是同一行的值。
我想转换我的代码,以便如果随后在此列中使用相同的字符串名称重复它,则可以排除键及其值。例如,如果我在一列中有两次字符串“ Name”,我想排除第一个“ Name”作为键字段,并将其(连同所有值)替换为后续的“ Name”,该名称后面可能有几行
到目前为止,我的代码:
com.amazonaws.auth.AWSCredentialsProvider
答案 0 :(得分:0)
也许您正在寻找这样的东西:
newOrderDetail
输出为:
import csv
# implementation following
def dict_of_dicts (filename, keyfield):
tabledict = {}
with open(filename, 'rt', newline='') as csv_file:
csvreader = csv.reader(csv_file, skipinitialspace=True)
for row in csvreader:
if len(row) < 1:
continue
tabledict[row[0]] = row[1:]
return tabledict
# example following
with open('test.csv', 'w') as fd:
w = csv.writer(fd)
w.writerows([
["Name","Andrew","John","Mike"],
["Age","20","25","26"],
["Weight","65","76","80"],
["Height","1.75","1.8","1","9"],
["Age","21","26","27"]
])
print(dict_of_dicts('test.csv', 'Name'))