2

I wrote the following python script that reads the contents of "prom output.csv" file, and after 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?

1 Answer 1

3

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

cr.writerow([k]+v) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.