【问题标题】:Need to store information about people in a file and pull out specific information (python)需要将有关人员的信息存储在文件中并提取特定信息(python)
【发布时间】:2020-04-24 06:21:57
【问题描述】:
我对 python 比较陌生,我需要制作的程序要求我将一组关于一个人的信息存储在一个文件中,该文件还包含关于其他人的相同信息。我想我必须使用二维字典或类似的东西,但我不确定。我需要能够:
- 将信息显示给一个人 - 最终在 GUI 中(还不知道如何制作 GUI,但我稍后会学习)
- 编辑一个人的任何信息
- 将任意数量的新人添加到此文件中
- 使用“列”中的所有信息并将其转换为自己的临时列表。例如,一个包含每个人年龄的列表。感谢您提供的任何帮助。
顺便说一句,我没有要显示的代码,我仍处于计划阶段。
【问题讨论】:
标签:
python
file
dictionary
【解决方案1】:
name = input("Name:\n") #Take in info
age = input("Age:\n")
hobby = input("Hobby:\n")
abc = open("test.txt", "a+") #Open the file
abc.write(name, age, hobby)
namesearch = input("Search: ") #Ask to search
with open("test.txt") as fo: #Open file
for rec in fo: #Start a for loop to search line by line
tokens = rec.strip().split(',', 2) # split to maximum three tokens
if tokens[0] == namesearch: #if token number 1(name)=namesearch then,
print(rec) #Return the whole line