1 回答

TA貢獻(xiàn)1891條經(jīng)驗(yàn) 獲得超3個(gè)贊
試試下面。對(duì)于問(wèn)題 1:您需要在打開(kāi)文件進(jìn)行寫(xiě)入時(shí)添加 - newline=''。對(duì)于第二個(gè)問(wèn)題:reader 對(duì)象需要先轉(zhuǎn)換為列表,然后才能與下標(biāo)一起使用。
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()
添加回答
舉報(bào)