Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 设置ID的项目_Python_Linux_Windows_Macos_Io - Fatal编程技术网

Python 设置ID的项目

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 我只是不知道如何让它存储信息,然后用另一个命令访问它。我将尝试将此代码用于排序系统,这将涉及更多内容,但我希望完成这

我很好奇我如何能创建一个文件中的数字/名称列表,以便以后访问它们。类似于为每个特定项目列出的ID列表。适用于python的所有示例:

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)