如何在一行中将输入写入文件并将多个输入存储到文件并能够读取它们?
How do I write input to files in one line and store multiple inputs to the file and be able to read them?
我是一名自学成才的程序员,我正在尝试在 Python 中制作一个票务系统,它接受多个输入并根据票数从文件中读取。但是,以前的输入被新的输入覆盖,我似乎无法修复它。
我得到的输出是这样的:
J
a
k
e
25
M
a
l
e
但我希望输出看起来像这样:
Jake;25;Male
我在下面附上了这个程序的代码。任何帮助将不胜感激。谢谢。
import sys, select, os
from os import system
def option_1():
with open(input("Input file name with extension: "), 'w+') as f:
people = int(input("\nHow many tickets: "))
name_l = []
age_l = []
sex_l = []
for p in range(people):
name = str(input("\nName: "))
name_l.append(name)
age = int(input("\nAge: "))
age_l.append(age)
sex = str(input("\nGender: "))
sex_l.append(sex)
f.flush()
for item in name:
f.write("%s\n" %item)
for item in [age]:
f.write("%s\n" %item)
for item in sex:
f.write("%s\n" %item)
x=0
print("\nTotal Ticket: ", people, '\n')
for p in range(1, people + 1):
print("Ticket No: ", p)
print("Name: ", name)
print("Age: ", age)
print("Sex: ", sex)
x += 1
def option_2():
with open(input('Input file name with extension: '), 'r') as f:
fileDir = os.path.dirname(os.path.realpath('__file__'))
f.flush()
f_contents = f.read()
print("\n")
print(f_contents, end = '')
def main():
system('cls')
print("\nTicket Booking System\n")
print("\n1. Ticket Reservation")
print("\n2. Read")
print("\n0. Exit Menu")
print('\n')
while True:
option = int(input("Choose an option: "))
if option < 0 or option > 2:
print("Please choose a number according to the menu!")
else:
while True:
if option == 1:
system('cls')
option_1()
user_input=input("Press ENTER to return to main menu: \n")
if((not user_input) or (int(user_input)<=0)):
main()
elif option == 2:
system('cls')
option_2()
user_input=input("Press ENTER to return to main menu: \n")
if((not user_input) or (int(user_input)<=0)):
main()
else:
exit()
if __name__ == "__main__":
main()
如果您有最新版本的 python,您可以使用 f-string
来编写您需要的格式。
您需要一个循环来迭代您收集的信息。
您可能只需要这个:
...
f.flush()
for name,age,sex in zip(name_l, age_l, sex_l):
f.write(f"{name};{age};{sex}\n")
...
此外,控制台的打印输出需要类似的循环:
print("\nTotal Ticket: ", people, '\n')
for p,(name,age,sex) in enumerate(zip(name_l, age_l, sex_l), start = 1):
print("Ticket No: ", p)
print("Name: ", name)
print("Age: ", age)
print("Sex: ", sex)
我是一名自学成才的程序员,我正在尝试在 Python 中制作一个票务系统,它接受多个输入并根据票数从文件中读取。但是,以前的输入被新的输入覆盖,我似乎无法修复它。
我得到的输出是这样的:
J
a
k
e
25
M
a
l
e
但我希望输出看起来像这样:
Jake;25;Male
我在下面附上了这个程序的代码。任何帮助将不胜感激。谢谢。
import sys, select, os
from os import system
def option_1():
with open(input("Input file name with extension: "), 'w+') as f:
people = int(input("\nHow many tickets: "))
name_l = []
age_l = []
sex_l = []
for p in range(people):
name = str(input("\nName: "))
name_l.append(name)
age = int(input("\nAge: "))
age_l.append(age)
sex = str(input("\nGender: "))
sex_l.append(sex)
f.flush()
for item in name:
f.write("%s\n" %item)
for item in [age]:
f.write("%s\n" %item)
for item in sex:
f.write("%s\n" %item)
x=0
print("\nTotal Ticket: ", people, '\n')
for p in range(1, people + 1):
print("Ticket No: ", p)
print("Name: ", name)
print("Age: ", age)
print("Sex: ", sex)
x += 1
def option_2():
with open(input('Input file name with extension: '), 'r') as f:
fileDir = os.path.dirname(os.path.realpath('__file__'))
f.flush()
f_contents = f.read()
print("\n")
print(f_contents, end = '')
def main():
system('cls')
print("\nTicket Booking System\n")
print("\n1. Ticket Reservation")
print("\n2. Read")
print("\n0. Exit Menu")
print('\n')
while True:
option = int(input("Choose an option: "))
if option < 0 or option > 2:
print("Please choose a number according to the menu!")
else:
while True:
if option == 1:
system('cls')
option_1()
user_input=input("Press ENTER to return to main menu: \n")
if((not user_input) or (int(user_input)<=0)):
main()
elif option == 2:
system('cls')
option_2()
user_input=input("Press ENTER to return to main menu: \n")
if((not user_input) or (int(user_input)<=0)):
main()
else:
exit()
if __name__ == "__main__":
main()
如果您有最新版本的 python,您可以使用 f-string
来编写您需要的格式。
您需要一个循环来迭代您收集的信息。
您可能只需要这个:
...
f.flush()
for name,age,sex in zip(name_l, age_l, sex_l):
f.write(f"{name};{age};{sex}\n")
...
此外,控制台的打印输出需要类似的循环:
print("\nTotal Ticket: ", people, '\n')
for p,(name,age,sex) in enumerate(zip(name_l, age_l, sex_l), start = 1):
print("Ticket No: ", p)
print("Name: ", name)
print("Age: ", age)
print("Sex: ", sex)