【问题标题】:Edit & save dictionary in another python file在另一个 python 文件中编辑和保存字典
【发布时间】:2016-12-07 00:09:28
【问题描述】:

我有 2 个 python 文件,file1.py 只有 1 个字典,我想从 file2.py 读取和写入该字典。两个文件都在同一个目录中。

我可以使用 import file1 从中读取,但我如何写入该文件。

片段:

file1.py(file1 中没有其他内容,除了以下数据)

dict1 = {
        'a' : 1,      # value is integer
        'b' : '5xy',   # value is string
        'c' : '10xy',
        'd' : '1xy',
        'e' : 10,
        }

file2.py

    import file1
    import json

    print file1.dict1['a']    #this works fine
    print file1.dict1['b']

    # Now I want to update the value of a & b, something like this:

    dict2 = json.loads(data)
    file1.dict1['a'] = dict2.['some_int']     #int value
    file1.dict1['b'] = dict2.['some_str']     #string value

我使用字典而不是文本文件的主要原因是,要更新的新值来自 json 数据,将其转换为字典更简单,每次我想更新时都无需解析字符串dict1

问题是,当我从 dict2 更新值时,我希望将这些值写入 file1 中的 dict1

此外,代码在 Raspberry Pi 上运行,我使用 Ubuntu 机器通过 SSH 连接到它。

有人可以帮我怎么做吗?

编辑:

  1. file1.py 可以保存为任何其他格式,例如 .json 或 .txt。我只是假设将数据作为字典保存在单独的文件中可以轻松更新。
  2. file1.py 必须是一个单独的文件,它是一个配置文件,所以我不想将它合并到我的主文件中。
  3. 上面提到的 dict2data 来自于套接字连接

dict2 = json.loads(data)

  1. 我想用来自套接字连接的数据更新 *file1**。

【问题讨论】:

  • 为什么不将数据写入两个文件都可以读取的 JSON?据我所知,让一个 python 文件编辑另一个文件并没有任何好处,这似乎真的没有必要

标签: python file dictionary fileupdate


【解决方案1】:

最后,正如@Zaren 所建议的,我在 python 文件中使用了 json 文件而不是字典。

这就是我所做的:

  1. file1.py 修改为 file1.json 并以适当的格式存储数据。

  2. file2.py,我在需要时打开 file1.json 而不是 import file1 并在 上使用 json.dumpjson.load file1.json

【讨论】:

    【解决方案2】:

    我认为您想将file1 中的数据保存到单独的.json 文件中,然后在第二个文件中读取.json 文件。以下是您可以执行的操作:

    file1.py

    import json
    dict1 = {
            'a' : 1,      # value is integer
            'b' : '5xy',   # value is string
            'c' : '10xy',
            'd' : '1xy',
            'e' : 10,
            }
    with open("filepath.json", "w+") as f:
        json.dump(dict1, f)
    

    这会将字典dict1 转储到json 文件中,该文件存储在filepath.json 中。

    然后,在你的第二个文件中:

    file2.py

    import json
    
    with open("pathname.json") as f:
        dict1 = json.load(f)
    
    # dict1 = {
            'a' : 1,      # value is integer
            'b' : '5xy',   # value is string
            'c' : '10xy',
            'd' : '1xy',
            'e' : 10,
            }
    
    dict1['a'] = dict2['some_int']     #int value
    dict1['b'] = dict2['some_str']     #string value
    

    注意:这不会更改您第一个文件中的值。但是,如果您需要访问更改后的值,您可以将您的数据dump 放入另一个json 文件,然后在需要数据时再次加载该json 文件。

    【讨论】:

    • 我想更改 file1 中的值,这就是我的问题所在。
    【解决方案3】:

    如果您尝试将字典打印回文件,您可以使用类似...

    outFile = open("file1.py","w")
    outFile.writeline("dict1 = " % (str(dict2)))
    outFile.close()
    

    最好有一个 json 文件,然后从文件中加载对象并将对象值写回文件。您可以让他们操作内存中的 json 对象,并简单地对其进行序列化。

    Z

    【讨论】:

    • 感谢 Zaren,正如您建议的那样,我将数据存储到 json 文件中,而不是 python 文件中的字典中。这让我的工作变得简单了很多。
    【解决方案4】:

    你应该使用pickle库来保存和加载字典https://wiki.python.org/moin/UsingPickle

    这里是pickle的基本用法

       1 # Save a dictionary into a pickle file.
       2 import pickle
       3 
       4 favorite_color = { "lion": "yellow", "kitty": "red" }
       5 
       6 pickle.dump( favorite_color, open( "save.p", "wb" ) )
    
    
    
       1 # Load the dictionary back from the pickle file.
       2 import pickle
       3 
       4 favorite_color = pickle.load( open( "save.p", "rb" ) )
       5 # favorite_color is now { "lion": "yellow", "kitty": "red" }
    

    【讨论】:

    • 谢谢詹姆斯,我研究了泡菜,如果我必须一直写一本完整的字典会很有用。但就我而言,我不确定是否必须写 1 或所有值。
    猜你喜欢
    • 1970-01-01
    • 2021-05-20
    • 2020-03-29
    • 2023-03-18
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多