0

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})
lena
  • 730
  • 2
  • 11
  • 23
  • https://stackoverflow.com/questions/2363731/append-new-row-to-old-csv-file-python – Shijith Apr 20 '20 at 09:53
  • Thanks, but I already did that, I need to write the file name in first column and s2 in second column – lena Apr 20 '20 at 10:01
  • use two `writer.writerow(....)` – Shijith Apr 20 '20 at 10:04
  • @Shijith, can you see my update, its ok writhing in two columns but in unarranged way! – lena Apr 20 '20 at 10:22
  • use `writer.writerow([file,s2])` please define `but in unarranged way`. please refer the third answer or the above link if you are using windows and having a newline problem – Shijith Apr 20 '20 at 10:29
  • OK ,I solve it, just I replased {} in [ ], but I have empty rows between the filled rows! – lena Apr 20 '20 at 10:34
  • please refer the third answer or the above link if you are using windows and having a newline problem add `newline=''` inside your open function – Shijith Apr 20 '20 at 10:37
  • Thanks a lot, you can put your comment as answer to get an acceptance answer :) – lena Apr 20 '20 at 10:41
  • ok added as answer – Shijith Apr 20 '20 at 10:58
  • Does this answer your question? [CSV file written with Python has blank lines between each row](https://stackoverflow.com/questions/3348460/csv-file-written-with-python-has-blank-lines-between-each-row) – sim Apr 20 '20 at 20:00

1 Answers1

0

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)

Shijith
  • 4,602
  • 2
  • 20
  • 34