我是python中的新手,我想转置CSV文件。
我的csv:
s:3:"ABC";
我想要的输出:
sphere,product
-9.00,COA-91391
-9.50,COA-91391
+0.50,COA-91392
+0.75,COA-91392
+1.00,COA-91392
如果有人可以就如何继续提供帮助。
提前致谢。
答案 0 :(得分:1)
with open('data.csv') as f:
lines = f.readlines()
lines = lines[1:] # skip header
result = dict()
for line in lines:
sphere, product = line.split(',')
product = product.strip()
if not result.has_key(product):
result[product] = list()
result[product].append(sphere.strip())
with open('data_out.csv', 'w') as f:
for product, spheres_list in result.items():
f.write("%s,%s\n" %(product, ','.join(spheres_list)))