为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

温温酱 2023-05-23 16:28:34
y我想收到我在 python shell 中键入的两次运行之间的时间(以秒为单位) 。很抱歉之前我没有具体说明我想要它是什么。基本上这是我正在测试在另一个大程序(比这个大)中实现的程序。这是我想要的输出:首先,我将运行该程序,它会询问我是否要借用,然后单击y。之后,我将再次运行该程序,它会要求我返回,我将再次单击y,它应该以秒为单位返回我借用的时间。循环将继续。这是我需要的图书馆管理系统程序。import timeimport csvdata_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 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

试试下面。对于问题 1:您需要在打开文件进行写入时添加 - newline=''。对于第二个问题:reader 对象需要先转换为列表,然后才能与下标一起使用。


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()


查看完整回答
反对 回复 2023-05-23
  • 1 回答
  • 0 关注
  • 87 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信