返回两次打开程序之间的时间(以秒为单位)



我想在python shell中键入y的两次运行之间获得以秒为单位的时间。

很抱歉,早些时候我没有具体说明我想要的是什么。基本上,这是我正在测试的另一个大程序(比这个大(中实现的程序。

以下是我想要的输出:

首先,我会运行程序,它会询问我是否想借用,我会单击y。之后,我将再次运行该程序,它会要求我返回,我会再次单击y,它应该以秒为单位返回我借用的时间。循环将继续。

这是我需要的图书馆管理系统的程序。

import time
import csv
data_backup1=[]
f=open("a1.csv",'r')
csvr=csv.reader(f)
for line in csvr:
#copying data into a temporary storage area from csv file
print(line)
data_backup1.append(line)
print(csvr,"this is csvr")    
f.close()
l=[]
if len(data_backup1)==0:
f=open("a1.csv",'w')
csvw=csv.writer(f)
a=input("Enter y to borrow")
if a=="y":
m="borrowing"
l.append(m)
print(l)
print("this is l")
n=time.time()
l.append(n)
print(l)
print("this is l")
csvw.writerow(l)
f.close()
f.close()    
f=open("a1.csv",'r')
csvr=csv.reader(f)
for line in csvr:
print(line)        
else:
a=input("Enter y to return")
if a=="y":
c=[]
f=open("a1.csv",'r')
csvr=csv.reader(f)
c=csvr[1]
print(c,"this is c")    
b=c[1]
print(b,"this is b")
b=int(b)
print(time.time()-b)
f.close()
f=open("a1.csv",'w')
f.close()

我想得到一些建议。

以下是我在两次跑步之间的实际收获。请注意,我已经创建了a1.csv

运行1次

<_csv.reader object at 0x00000231EA788640> this is csvr
Enter y to borrowy
['borrowing']
this is l
['borrowing', 1597526322.2194974]
this is l
['borrowing', '1597526322.2194974']
[]

在运行1中,我不知道为什么要添加另一个[],所以请在这方面提供帮助。

运行2-在这里我希望它返回时间,但得到一个错误:

['borrowing', '1597526322.2194974']
[]
<_csv.reader object at 0x0000018A1B2E8640> this is csvr
Enter y to returny
Traceback (most recent call last):
File "C:UsersCCFFINAppDataLocalProgramsPythonPython38test.py", line 39, in <module>
c=csvr[1]
TypeError: '_csv.reader' object is not subscriptable

我在一些地方使用了print来识别根本不必要的错误。

此外,如果可能,请建议其他方法来测量两个连续数据输入之间的时间差(以秒为单位(。

请尝试以下操作。对于问题1:您需要在打开文件进行写入时添加-newline=''。对于第二期:阅读器对象需要转换为列表,然后才能与下标一起使用。

import csv
import os
import time
data_backup1=[]
l=[]
file_exists = os.path.exists('a1.csv')
if file_exists:
f=open("a1.csv",'r')
csvr=csv.reader(f)
for line in csvr:
#copying data into a temporary storage area from csv file
print(line)
data_backup1.append(line)
print(csvr,"this is csvr")
f.close()
if len(data_backup1)==0:
f=open("a1.csv",'w',newline='')
csvw=csv.writer(f)
a=input("Enter y to borrow")
if a=="y":
m="borrowing"
l.append(m)
print(l)
print("this is l")
n=round(time.time())
l.append(n)
print(l)
print("this is l")
csvw.writerow(l)
f.close()
f.close()
f=open("a1.csv",'r')
csvr=csv.reader(f)
for line in csvr:
print(line)
else:
a=input("Enter y to return")
if a=="y":
c=[]
f=open("a1.csv",'r')
csvr=csv.reader(f)
line=list(csvr)
c=line[0]
print(c,"this is c")
b=c[1]
print(b,"this is b")
b=int(b)
print(round(time.time())-b)
f.close()
f=open("a1.csv",'w')
f.close()

最新更新