Python 返回两次打开程序之间的时间(秒)
我希望在pythonshell中键入Python 返回两次打开程序之间的时间(秒),python,csv,time,backup,Python,Csv,Time,Backup,我希望在pythonshell中键入y,以秒为单位接收两次运行之间的时间 很抱歉,之前我没有具体说明我希望它是什么。 基本上,这是我正在测试的程序,要在另一个大程序(比这个大)中实现 以下是我想要的输出: 首先,我将运行该程序,它将询问我是否要借用,我将单击y。 之后,我将再次运行该程序,它将要求我返回,我将再次单击y,它将以秒为单位返回我借用的时间。这一循环将继续下去 这是一个程序,我需要一个图书馆管理系统 import time import csv data_backup1=[] f=op
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
这里是csvr
输入y以借用
[“借用”]
这是我
[“借款”,1597526322.2194974]
这是我
['借用','1597526322.2194974']
[]
在运行1中,我不知道为什么要添加另一个[]
,因此也请在这方面提供帮助
运行2-在这里我希望它返回时间,但我得到一个错误:
['following','1597526322.2194974']
[]
这是csvr
输入y返回
回溯(最近一次呼叫最后一次):
文件“C:\Users\CCFFIN\AppData\Local\Programs\Python\Python38\test.py”,第39行,在
c=csvr[1]
TypeError:“\u csv.reader”对象不可订阅
我在一些地方使用了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()
谢谢你,但是你能不使用“尝试”给我这个程序吗。。。事实上我不知道如何使用try。和#f=open(“a1.csv,'w')#f.close()我保留了它以删除文件中的记录,如果您有其他方法可以这样做,请也给我。还有newline做什么呢..我在末尾加回了空文件写入,并且删除了try块。但是,最终您必须学会尝试,除了确保处理所有运行时错误之外,例如:在第一步中,您假设a1.csv文件始终可供读取,如果它不可读取怎么办。有其他方法可以处理这些错误,就像我在这里使用
os.path.exists
所做的那样。但是,你必须事先知道所有的错误。有关换行符,请检查本页的脚注。