我试图让这段代码询问用户的详细信息,然后用逗号分隔字符串将它们保存到.txt文件中。每次运行代码时我都需要写一个新行,但是添加" / n"在字符串的末尾但是它在同一行上提供了所有用户的数据。有什么帮助吗?
print ("enter your name, age, and year group and password")
while True:
reg_name = input("Name:"))
reg_pass = input ("Password:")
reg_age = input ("age:")
reg_group = input ("Year Group")
print ("Is this infomation correct?")
print ("Name:",reg_name)
print ("password:",reg_pass)
print ("Age:",reg_age)
print ("Year Group:", reg_group)
reg_correct = input ("[Y/N]").lower()
if reg_correct == "y":
reg_user = reg_name[0:3]+reg_age
reg_write = open("D:\\Computer science\\Computing test\\logindata.txt","a")
reg_write.write (reg_user+","+reg_name+","+reg_pass+","+reg_age+","+reg_group+"/n")
print ("Your username is:",reg_user)
reg_write.close()
break
elif reg_correct == "n":
print ("Please Re-enter your infomation")
else:
Print ("Invalid input! Please try again...!")
答案 0 :(得分:0)
我认为您可能需要\n
而不是/n
。它是实际的换行符。否则,它将在文件中的每个语句之间添加“/ n”。 \n
创建换行符。
print ("enter your name, age, and year group and password")
while True:
reg_name = input("Name:"))
reg_pass = input ("Password:")
reg_age = input ("age:")
reg_group = input ("Year Group")
print ("Is this infomation correct?")
print ("Name:",reg_name)
print ("password:",reg_pass)
print ("Age:",reg_age)
print ("Year Group:", reg_group)
reg_correct = input ("[Y/N]").lower()
if reg_correct == "y":
reg_user = reg_name[0:3]+reg_age
reg_write = open("D:\\Computer science\\Computing test\\logindata.txt","a")
reg_write.write (reg_user+","+reg_name+","+reg_pass+","+reg_age+","+reg_group+"\n")
print ("Your username is:",reg_user) # ^^^
reg_write.close()
break
elif reg_correct == "n":
print ("Please Re-enter your infomation")
else:
Print ("Invalid input! Please try again...!")
答案 1 :(得分:0)
不同的方式:
您还可以使用 writelines()方法避免转义字符。
reg_write.writelines (reg_user+","+reg_name+","+reg_pass+","+reg_age+","+reg_group)
答案 2 :(得分:0)
您可以使用"babel-plugin-react-html-attrs"
"babel-plugin-transform-class-properties"
"babel-plugin-transform-decorators-legacy"
"babel-preset-es2015"
"babel-preset-react"
功能将信息写入文件。通过使用print
模块,您可以轻松运行一些错误检查,以帮助验证您的文件是否可访问。使用Python最近添加的格式字符串,打印输出变量非常容易。
pathlib