Python、repl.it - 未使用 csv.writer 和 writer.writerow 将详细信息写入文件
Python, repl.it - details are not being written to file using csv.writer and writer.writerow
我有以下 repl.it 程序,请注意该程序的注册部分之前工作正常,但现在停止工作了。
它到了最后说“写入文件”的地方,但由于没有任何内容写入文本文件,实际的 write-rows 命令不知何故被跳过。
整个程序在这里:
https://repl.it/@oiuwdeoiuas/Matchmakingskills-1
代码的相关部分如下,尽管可能还有其他因素(因此提供了整个代码)
def register():
print("===Register====")
print("First things first, sign up and tell us a little about yourself")
with open("dating.txt","a") as fo:
writer=csv.writer(fo)
firstname=input("Enter first name:")
lastname=input("Enter last name:")
username=firstname+lastname[0]+"bird"
print("Your automatically generated username is:",username)
password=input("Enter password:")
gender=input("Enter gender")
email=input("Enter email:")
dob=input("Enter date of birth in format dd/mm/yy:")
beliefs=input("Enter beliefs")
strengthslist=["patience","efficiency","sensitivity","frankness","submissiveness","leadership","timekeeping","laidback"]
print(strengthslist)
strengths=input("Enter your top strength: (select from the above list)")
contactcount=0
writer.writerow([username,password,firstname,lastname,gender,email,dob,beliefs,strengths,contactcount])
print("written to file")
mainmenu()
我可能是错的,但是你需要 writerows
而不是 writerow
因为 writerows
像你的一样接受列表而 writerow
接受列。
您正在尝试读取一个仍处于打开状态的文件:
def register():
# ...
with open("dating.txt","a") as fo:
# ...
print("written to file")
# At this point, "dating.txt" hasn't been written to
# the next call to open it that occurs here will
# see the state of the file either partially written, or before
# the row is written at all
mainmenu()
有几个解决方案。最快的方法是在此处取消缩进 mainmenu()
一级:
def register():
# ...
with open("dating.txt","a") as fo:
# ...
print("written to file")
# dating.txt has been closed now, it's safe to read it
mainmenu()
当下一个方法出现并尝试读取文件时,它将以这种方式包含预期的数据。
我有以下 repl.it 程序,请注意该程序的注册部分之前工作正常,但现在停止工作了。
它到了最后说“写入文件”的地方,但由于没有任何内容写入文本文件,实际的 write-rows 命令不知何故被跳过。
整个程序在这里:
https://repl.it/@oiuwdeoiuas/Matchmakingskills-1
代码的相关部分如下,尽管可能还有其他因素(因此提供了整个代码)
def register():
print("===Register====")
print("First things first, sign up and tell us a little about yourself")
with open("dating.txt","a") as fo:
writer=csv.writer(fo)
firstname=input("Enter first name:")
lastname=input("Enter last name:")
username=firstname+lastname[0]+"bird"
print("Your automatically generated username is:",username)
password=input("Enter password:")
gender=input("Enter gender")
email=input("Enter email:")
dob=input("Enter date of birth in format dd/mm/yy:")
beliefs=input("Enter beliefs")
strengthslist=["patience","efficiency","sensitivity","frankness","submissiveness","leadership","timekeeping","laidback"]
print(strengthslist)
strengths=input("Enter your top strength: (select from the above list)")
contactcount=0
writer.writerow([username,password,firstname,lastname,gender,email,dob,beliefs,strengths,contactcount])
print("written to file")
mainmenu()
我可能是错的,但是你需要 writerows
而不是 writerow
因为 writerows
像你的一样接受列表而 writerow
接受列。
您正在尝试读取一个仍处于打开状态的文件:
def register():
# ...
with open("dating.txt","a") as fo:
# ...
print("written to file")
# At this point, "dating.txt" hasn't been written to
# the next call to open it that occurs here will
# see the state of the file either partially written, or before
# the row is written at all
mainmenu()
有几个解决方案。最快的方法是在此处取消缩进 mainmenu()
一级:
def register():
# ...
with open("dating.txt","a") as fo:
# ...
print("written to file")
# dating.txt has been closed now, it's safe to read it
mainmenu()
当下一个方法出现并尝试读取文件时,它将以这种方式包含预期的数据。