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

y我想收到我在 python shell 中键入的两次运行之间的时间(以秒为单位) 。

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

这是我想要的输出:

首先,我将运行该程序,它会询问我是否要借用,然后单击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.


温温酱
浏览 93回答 1
1回答

万千封印

试试下面。对于问题 1:您需要在打开文件进行写入时添加 - newline=''。对于第二个问题:reader 对象需要先转换为列表,然后才能与下标一起使用。import csvimport osimport timedata_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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python