簡體   English   中英

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

[英]Writing in specific columns in csv file in python

我想用這種方式在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})

你可以做如下。 參考這里

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

newline=''是為了避免寫入不需要的換行符(在 Windows 上,行結尾是\r\n

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM