python - Write in csv file with dict -
i'm using python 2.7 , need make dataset , have dictionary i'll put content in csv file. current dictionary:
dict = {"arthur1.jpg": [0.123,0.456,0.864], "arthur2.jpg": [0.124,0.764,0.965]}
in csv file this:
arthur1.jpg 0.123 0.456 0.864 arthur2.jpg 0.124 0.764 0.965
the first column dicts's keys be, , next set of columns in same line values of respective key.
turn each key-value pair row; values list, prepend list list literal containing key:
import csv open(youcsvfilename, 'rb') csvf: csvwriter = csv.writer(csvf) filename, values in dictionary.items(): csvwriter.writerow([filename] + values)
the expression [filename] + values
produces on new list serves output row. csv
module takes list , writes in correct format csv file.
Comments
Post a Comment