I want to recieve the time in seconds between the two runs where I type y
in the python shell.
I am sorry earlier I did not specify what I want it to be. Basically this is the program I am testing to implement in another big program (bigger than this one).
Here is what I want the output to be:
First I will run the program and it will ask if I want to borrow and I will click y
.
After that I will run the program again and it will ask me to return, again I will click y
and it should return the time in seconds for which I borrowed. the cycle will continue.
This is the program which I need for a library management system.
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()
I would like to get some suggestions.
Here is what I actually got in between the two runs.
Please note that I have already created a1.csv
.
run 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']
[]
In run 1 I don't know why another []
is being added so please also help in that area.
run 2 - here I want it to return the time but am getting an error:
['borrowing', '1597526322.2194974']
[]
<_csv.reader object at 0x0000018A1B2E8640> this is csvr
Enter y to returny
Traceback (most recent call last):
File "C:\Users\CCFFIN\AppData\Local\Programs\Python\Python38\test.py", line 39, in <module>
c=csvr[1]
TypeError: '_csv.reader' object is not subscriptable
I used print
in some places to identify the error which are not at all necessary.
Also, if possible, please suggest other methods of measuring the time difference (in seconds) between two successive data entry.
Try below. For issue 1: you need to add - newline='' while opening the file for write. And for 2nd issue: reader object needs to be converted to list before it can be used with subscripts.
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()