简体   繁体   中英

Writing in specific columns in csv file in python

I want to write my output code in csv file in this way, first column in csv contain file name and second column contain the value of exetime (s2), how can do that?

 inputpath = 'C:/Users/mach/Desktop/hh/c*.csv'
    for file in iglob(inputpath):
        s1 = time.time()
        size = function(file) 
        s2 = time.time() - s1 
        with open(r'C:/Users/mach/Desktop/exetime.csv','a') as f:
            writer = csv.writer(f)
            writer.writerow({file,s2})

you can do as below. refer here

with open(r'C:/Users/mach/Desktop/exetime.csv','a',newline='') as f:
            writer = csv.writer(f)
            writer.writerow([file,s2])

newline='' is to avoid writing a unwanted newline (line ending is \r\n on Windows)

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