Python:将处理时间添加到输出CSV文件

时间:2016-06-10 00:19:17

标签: python csv time

下面的代码创建了一个pandas数据帧并将其输出到csv文件中。它还计算处理时间。如何将处理时间添加到csv文件?

感谢您的时间。

import pandas as pd
import csv
import time
def procedure():
    time.sleep(0)
t0 = time.clock()
y=pd.DataFrame({'one':[1,2,3,4],'two':[3,5,7,5],'three':[2,3,4,9],'four':[4,3,1,0],})
y.to_csv('temp1.csv')
procedure()
print (time.clock()-t0)

3 个答案:

答案 0 :(得分:0)

以追加模式打开文件,将时间添加到结尾:

...
procedure()
elapsed = time.clock()-t0
with open('temp1.csv', 'a') as f:
    f.write(str(elapsed))

答案 1 :(得分:0)

import time
start_time = time.time()
finish_time = time.time() - start_time

要获得处理时间,您可以使用以下代码:

import pandas as pd
import csv
import time
def procedure():
    time.sleep(0)
start_time = time.time()
y=pd.DataFrame({'one':[1,2,3,4],'two':[3,5,7,5],'three':[2,3,4,9],'four':[4,3,1,0],})
y.to_csv('temp1.csv')
procedure()
finish_time = time.time() - start_time
print (finish_time)

答案 2 :(得分:0)

您的缩进已关闭:

import pandas as pd
import csv
import time

def procedure():
    time.sleep(0)
    y=pd.DataFrame({'one':[1,2,3,4],'two':[3,5,7,5],'three':[2,3,4,9],'four':[4,3,1,0],})
    y.to_csv('temp1.csv')

start_time = time.time()    
procedure()
finish_time = time.time() - start_time
print (finish_time)