我有一个简单的问题。我正在使用此代码来存储csv文件中的词典词典。我如何更改存储字典从行到列使用Python的类型
data = dataA,dataB, dataC, dataD
w = csv.writer(open("test.csv", "w"))
for x in data:
for field, possible_values in x.items():
print(field, possible_values)
w.writerow([field, possible_values])
我在csv中获得的存储值存储在行中,但我希望它们存储为列。
我的实际结果CSV:
name: Alex
Old: 22
我的CSV目标应该是这样的:
Name Old
Alex 22
我怎样才能改变呢?
UPDATE1:
线索是for key in x.keys()
。经过几个小时的努力,我更新了我的代码,它比以前更好,但我仍然有一个小问题,在存储所有值和关键字x.keys()和y.values() 。我的csv文件dictioanries的
if not os.path.isfile(filename):
outfile = open(filename, "w")
#outfile.write("#Sequence,,,,")
for x in data:
print(x.keys())
for key in x.keys():
print(key)
store_key= key + ","
outfile = open(filename, "a")
outfile.write(store_key)
outfile.close()
for y in data:
print(y.values())
for value in y.values():
print(value)
store_value = value + ","
outfile = open(filename, "a")
outfile.write(store_value)
outfile.close
现在我需要与键和值之间的“\ n”也许单独拿到钥匙的线下的值OD一阳指。
任何帮助不胜感激。
发布多字典的预期输出 – RomanPerekhrest