【问题标题】:Setting items for IDsID的设置项目
【发布时间】:2017-09-21 09:07:12
【问题描述】:

很好奇我如何能够将数字/名称列表创建到文件中以便以后访问它们。就像每个特定项目的 ID 列表一样。全部用于 python EX:

IDs = ('open', 'IDFile.txt', 'a') #For storing info
info = input(str("Enter ID: ") #ID for storing, can be number or letter

我只是不确定如何让它存储信息并稍后使用另一个命令访问它。我将尝试将此代码用于涉及更多内容的分类系统,但我想完成这部分。

【问题讨论】:

标签: python linux windows macos io


【解决方案1】:

我会使用字典来存储基于字符串和整数的 id。然后我会使用 JSON 将数据写入文件或从文件中写入数据。

存储 ID

import json
ids = {
        "id1":45,
        "id2":"john"
    }
with open("IDFile.json","w") as fo:
    json.dump(ids,fo)

检索 ID

import json
fetchedIds={}
with open("IDFile.json","r") as fi:
    fetchedIds = json.loads(fi.read())
print(fetchedIds)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 2014-09-09
    • 1970-01-01
    • 2011-07-08
    • 2018-05-05
    • 1970-01-01
    相关资源
    最近更新 更多