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()
添加回答
举报