If you have a python list of lists, how do you turn it into comma-separated variables format?
First you need the library:
It’s pretty straight-forward after that:
def convert_list_to_csv(list_of_data): header = ["Board Name", 'Month', "Backlog", "Backlog_pri", "In Progress", "In Progress_pri", "Coding Done", "Coding Done_pri", "QA", "QA_pri", "Final QA", "Final QA_pri", "QA Approved", "QA Approved_pri", "Prod Deployed", "Prod Deployed_pri"] list_of_data.insert(0, header) with open("output.csv", "w") as f: writer = csv.writer(f) writer.writerows(list_of_data) return "output.csv"
I accidentally duplicated this method so this is actually the first of two I wrote. Duplication is a bad idea, so I’m going to go back and get rid of one.
This one is the larger one. It takes in a list of lists (list_of_data) and then inserts the header list at the very front.
.insert(x, y) where x is the index location in the list and y is the object you are inserting into the list.
Then you open a .csv file (just having this will create one if there isn’t one there):
with open("output.csv", "w") as f: writer = csv.writer(f) writer.writerows(list_of_data) return "output.csv"
So you open the file as f, and then create a writer variable that is the csv object with the method writer being called on the file f.
csv.writer(csvfile, dialect='excel', **fmtparams) Return a writer object responsible for converting the user’s data into delimited strings on the given file-like object.
Then you call the writer method “writerows”.
csvwriter.writerows(rows) Write all the rows parameters (a list of row objects as described above) to the writer’s file object, formatted according to the current dialect.
The dialect is set by default to ‘excel’ which is handy since that’s what I was aiming for the data to be used as. I fed in my list of lists which the writerows treats every list as a row.