【问题标题】:How to reload text file into JSON after text file is edited in Python?在 Python 中编辑文本文件后如何将文本文件重新加载到 JSON 中?
【发布时间】:2021-06-07 10:22:43
【问题描述】:

我想在我的文本文件更新后通过重新打开文本文件来重新加载我的 JSON。

import json

with open('np.txt') as np:
    np_data = np.read()
np_list=json.loads(np_data)

def reopen_file():
    print("reloading")
    with open('np.txt') as np:
        np_data = np.read()
    np_list=json.loads(np_data)



y=1
while(y==1):
    #np.flush()
    #reopen_file()
    x=input("input: ")
    print("your input is" +x)
    if(int(x)==1):
        print(np_list)
        y=input("continue?: ")

reopen_file()
print(np_list)

输出:

我在输入“y”的值之前更新了文本文件(继续?),但输出保持不变。 (我正在手动编辑文件)

np.txt:

{"a":"1",
"b":"2",
"c":"3",
"d":"4",
"e":"5",
"f":"6",
"g":"7",
"h":"8",
"i":"9"}

【问题讨论】:

  • 为什么文件会改变?你没有在任何地方编辑它。此外,没有代码或终端输出的图像! >:(
  • @jemand771 我正在手动编辑文件,而不是在代码中
  • @jemand771 您没有更新实际的np_list,只需将其返回并设置在循环中即可。
  • 请查看one
  • 题外话:您可以使用json.load() 一步读取JSON 文件,而不是您当前正在执行的两个步骤(即file.read() 后跟json.loads())。

标签: python json


【解决方案1】:

正如我已经评论过的,您的代码实际上并没有更新您正在使用的同一个 np_list。一个快速而肮脏的例子就是在reopen_file的末尾添加一个print(np_list)

input: 1
your input is1
{'e': '5', 'b': '2', 'd': '4', 'f': '5', 'c': '3', 'a': '1', 'h': '8', 'i': '9', 'g': '7'}
continue?: 1
reloading
{'e': '5', 'b': '2', 'd': '4', 'f': '6', 'c': '3', 'a': '1', 'h': '8', 'i': '9', 'g': '7'}
{'e': '5', 'b': '2', 'd': '4', 'f': '5', 'c': '3', 'a': '1', 'h': '8', 'i': '9', 'g': '7'}

这个解决方案很好用:

import json


def reopen_file():
    with open('np.txt') as np:
        np_data = np.read()
    return json.loads(np_data)


np_list=reopen_file()


y=1
while(y==1):
    #np.flush()
    #reopen_file()
    x=input("input: ")
    print("your input is" +x)
    if(int(x)==1):
        print(np_list)
        y=int(input("continue?: "))
    np_list = reopen_file()

哪些输出:

Python-reload-file> python test.py
input: 1
your input is1
{'d': '4', 'b': '2', 'f': '5', 'h': '8', 'e': '5', 'a': '1', 'c': '3', 'i': '9', 'g': '7'}
continue?: 1
input: 1
your input is1
{'d': '4', 'b': '2', 'f': '6', 'h': '8', 'e': '5', 'a': '1', 'c': '3', 'i': '9', 'g': '7'}
continue?:

【讨论】:

  • 我正在为我的应用程序进行测试,它将有 99% 的阅读率和只有 1% 的更新。所以,我只想在偶尔更新文件时更新 np_list 。同样的 np_list 用于其他读取功能。您的方法会自动更新在其他函数中读取的全局 np_list 吗?
  • 如果你使用全局 np_list 是的。我认为最好使用一个将信息封装起来的对象,然后调用它的更新以重新加载数据。
【解决方案2】:

np_list 被声明为一个全局变量,如果不使用 global 关键字就无法覆盖该变量。简化示例:

a = 3
def change_a():
    a = 4
    print("inside:", a)

print("outside:", a)
change_a()
print("outside after:", a)

输出:

outside: 3
inside: 4
outside after: 3

使用global 关键字的示例:(如果您有其他选择,例如创建课程,请不要这样做)

a = 3
def change_a():
    global a
    a = 4
    print("inside:", a)

print("outside:", a)
change_a()
print("outside after:", a)

输出:

outside: 3
inside: 4
outside after: 4

关于如何读取 json 文件的另一个小提示:而不是做

import json
with open('np.txt') as np:
    np_data = np.read()
np_list=json.loads(np_data) 

你可以

with open("np.txt") as np:
    np_list = json.load(np)

【讨论】:

  • 我在其他函数中使用 np_list。从第二个提示来看,np_list 不会在其他函数中无法访问,因为它不是全局变量吗?
  • @DeadPool 取决于你在哪里做。如果您只是在全局 scpoe 中(而不是在函数内部)执行此操作,则 with 语句无关紧要。仅仅因为它是缩进的并不意味着它是一个不同的范围。当然,如果你在函数中使用这个语句,你将不得不再次使用global关键字或者使用other answer的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 2021-07-31
  • 2012-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多