简体   繁体   中英

How to write my results in two rows of a .csv file in python?

I have two lists one containing the file name and the other containing its corresponding value. I want to save the data in a CSV file. With the two headers: Filename, value . The below mentioned code saves the data in just two columns.

file_name = []
values = []

# And then when writing to file:
with open('./output.csv','w') as result_file:
    wr = csv.writer(result_file,delimiter=' ',quotechar='\\',quoting=csv.QUOTE_MINIMAL)
    wr.writerows([file_name,map(lambda x: [x],values)])

Use zip() to pair up the corresponding elements of the two lists.

wr.writerow(['Filename', 'value']) # Write header line
wr.writerows(zip(file_name, values))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM