import csv
nome = input("nome: ")
numero1 = input("Inscrição: ")
secao = input("Seção: ")
local = input("local: ")
continuar = input("continuar? S/N: ")
while(continuar == "S"):
nome = input("nome: ")
numero1 = input("Inscrição: ")
secao = input("Seção: ")
local = input("local: ")
continuar = input("continuar? S/N: ")
v = open("valores", "w")
writer = csv.writer(v)
writer.writerow([nome, numero1, secao, local])
else:
print("ok")
如果是葡萄牙语,请忽略。我试图将变量按顺序写入 csv 文件中,但当我运行它时,它只写入一行,并不断删除和重写它(我希望它处于循环状态,直到“连续”== N 喜欢
nome: asdjhagsdjh
numero1: kasjhdkhasdkl
secao: sajfdksdh
local: asjfkjfhsdkj
continuar? S/N: S
(asdjhagsdjh,kasjhdkhasdkl,sajfdksdh,asjfkjfhsdkj)
nome: asdjhagsdjh
numero1: kasjhdkhasdkl
secao: sajfdksdh
local: asjfkjfhsdkj
continuar? S/N: N
(asdjhagsdjh,kasjhdkhasdkl,sajfdksdh,asjfkjfhsdkj
asdjhagsdjh,kasjhdkhasdkl,sajfdksdh,asjfkjfhsdkj)
我现在就开始,如果有什么愚蠢的事情:我很抱歉
每次写入都会擦除现有文件。 如果您不想这样做,请使用
'a'
(追加)。 您可能想要添加代码来清除文件(如果它已经存在),但我将把它作为练习:
import csv
# Open the file once, all writes will be to the end of the file.
# 'with' will close the file when its block exits.
with open('values.csv', 'a') as v:
writer = csv.writer(v)
continue_ = 'y' # to enter the while loop, assume y for first pass
while continue_.lower().startswith('y'): # handles Y, Yes, YES, yes, y, yeppers, etc.
name = input('Name: ')
registration = input('Registration: ')
section = input('Section: ')
location = input('Location: ')
writer.writerow([name, registration, section, location])
continue_ = input('Continue? Y/N: ') # NOW ask to continue
else:
print('ok')