Adriano10 Adriano10 - 2 months ago 7
Python Question

shell shift in exel using python

i wrote the following python script that reads the contents of "prom output.csv" file" and after does some processing it writes the output to the file "sorted output."

import collections
import csv
import sys

with open("prom output.csv","r") as f:
cr = csv.reader(f,delimiter=",")
d=collections.defaultdict(lambda : list())
header=next(cr)
for r in cr:
d[r[0]].append(r[1])


with open("sorted output.csv","w") as f:
cr = csv.writer(f,sys.stdout, lineterminator='\n')
cr.writerow(header)
od = collections.OrderedDict(sorted(d.items()))
for k,v in od.items():
cr.writerow([k,";".join(v)])


The output "sorted output.csv" looks like:

enter image description here

My input file: "prom output.csv" looks like:

enter image description here

I want to slightly modify the current output so that it will have the following form:

enter image description here

Any ideas?

Answer

Don't use join for your row content; just combine the header w/ the data into a single list:

cr.writerow([k]+v)