Python 设置ID的项目
我很好奇我如何能创建一个文件中的数字/名称列表,以便以后访问它们。类似于为每个特定项目列出的ID列表。适用于python的所有示例:Python 设置ID的项目,python,linux,windows,macos,io,Python,Linux,Windows,Macos,Io,我很好奇我如何能创建一个文件中的数字/名称列表,以便以后访问它们。类似于为每个特定项目列出的ID列表。适用于python的所有示例: IDs = ('open', 'IDFile.txt', 'a') #For storing info info = input(str("Enter ID: ") #ID for storing, can be number or letter 我只是不知道如何让它存储信息,然后用另一个命令访问它。我将尝试将此代码用于排序系统,这将涉及更多内容,但我希望完成这
IDs = ('open', 'IDFile.txt', 'a') #For storing info
info = input(str("Enter ID: ") #ID for storing, can be number or letter
我只是不知道如何让它存储信息,然后用另一个命令访问它。我将尝试将此代码用于排序系统,这将涉及更多内容,但我希望完成这部分 我会使用字典来存储字符串和基于整数的id。然后,我将使用JSON向文件写入数据,并从文件写入数据 存储Id 检索Id的 看看
import json
ids = {
"id1":45,
"id2":"john"
}
with open("IDFile.json","w") as fo:
json.dump(ids,fo)
import json
fetchedIds={}
with open("IDFile.json","r") as fi:
fetchedIds = json.loads(fi.read())
print(fetchedIds)