简体   繁体   English

在 python 的 csv 文件中写入特定列

[英]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?我想用这种方式在csv文件中写我的output代码,csv中的第一列包含文件名,第二列包含exetime(s2)的值,怎么办?

 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) newline=''是为了避免写入不需要的换行符(在 Windows 上,行结尾是\r\n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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