Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.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 使用JSON文件存储和检索列表数据_Python_Json - Fatal编程技术网

Python 使用JSON文件存储和检索列表数据

Python 使用JSON文件存储和检索列表数据,python,json,Python,Json,每次在下一个程序执行/会话期间提取JSON文件时,都会覆盖以前的数据。最终,我要做的是将数据保存到一个文件中,并在下次运行程序时检索它。每当我运行程序时,我都想将数据附加到列表中 import json import os.path income = [] def file_exists(): if os.path.exists('income.json') == True: with open('income.json') as f: inc

每次在下一个程序执行/会话期间提取JSON文件时,都会覆盖以前的数据。最终,我要做的是将数据保存到一个文件中,并在下次运行程序时检索它。每当我运行程序时,我都想将数据附加到列表中

import json
import os.path

income = []

def file_exists():
    if os.path.exists('income.json') == True:
        with open('income.json') as f:
            income = json.load(f)
    else:
        pass

def desire_func():
  x = input('What do you want to do? ')
  if x != 'n':
    transactions()
    print(income)
  else:
    return

def transactions():
    with open('income.json', 'w') as g:
        entry = float(input('Transaction info: '))
        income.append(entry)
        json.dump(income, g)
        desire_func()

file_exists()
transactions()
print(income)
任何帮助都将不胜感激,因为我试图从多个角度解决这个问题,并且总是遇到不同的问题。起初,我试图整理数据,这似乎不太可靠,但我愿意接受任何可能有效的方法

提前感谢你的帮助


编辑:添加到desire_func()中,以便在排除故障时可以轻松地多次添加到同一列表(income)。

使用附加模式
a
而不是写入模式
w

更改此项:

with open('income.json', 'w') as g:
为此:

with open('income.json', 'a') as g:
编辑:

每次程序结束时,列表都会初始化。您可以使用循环继续读取文件,除非您不想,例如:

income = []

def populate_list():
    income.append(input('Enter something to add to list: '))

def show_list():
    print(income)

while True:
    x = input("Press 1 to exit: ")
    if x is not'1':
        populate_list()
        show_list()
    else:
        break

似乎您希望将加载的JSON设置到income中,而不是追加,以防止每次迭代都将数组嵌套得越来越深

def file_exists():
if os.path.exists('income.json') == True:
    with open('income.json') as f:
        income (json.load(f))
else:
    pass
应该成为

def file_exists():
if os.path.exists('income.json') == True:
    with open('income.json') as f:
        income = json.load(f)
else:
    pass
然后,收入变量的列表形式因子应保留在JSON文件中

例如:

>>> testlist = ['test', 'test2']
>>> import json
>>> with open('income.json', 'w') as g:
...     json.dump(testlist, g)
...
退出并重新启动

>>> import json
>>> with open('income.json') as f:
...     testlist = json.load(f)
... 
>>> testlist
['test', 'test2']
编辑: 所以,我认为有一件事可能是个问题,那就是关于变量范围。函数通常采用参数。这样可以确保在代码中的其他位置使用相同的变量名时,函数变量的值是明确的。使用当前代码,如果在transactions函数中打印收入变量,您将看到它是一个空列表

可以通过将列表传递给函数来解决此问题:

import json
import os.path

def file_exists():
    if os.path.exists('income.json') == True:
        with open('income.json') as f:
            income = json.load(f)
            print(income)
            return income
    else:
        return []

def transactions(income):
    print(income)
    with open('income.json', 'w') as g:
        entry = float(input('Transaction info: '))
        print(entry)
        income.append(entry)
        print(income)
        json.dump(income, g)

income = file_exists()
transactions(income)
print(income)

你也可以考虑使用泡菜或莳萝来代替JSON文件。我当然喜欢这个方法,但是我看到结果每次都附上一个新的列表。我的目标是附加到现有列表中。抱歉问题不清楚。@SteveSzuter我在编辑中添加了一个示例来解释另一个选项。这看起来非常好,但我仍然需要将数据附加到JSON文件中。我想这才是我真正遇到麻烦的地方。如何将收入列表保存到JSON文件中,以便下次执行程序时加载数据,它包含列表的所有元素。未经请求的建议:我建议使用两个或三个函数:load()、save()和(可选)add()。load会打开json文件并将其存储到变量中,add会从input添加到该文件中,save会将其存储回文件中。谢谢您的建议!实际上,“file_exists()”是load()函数,save()/add()都在“transactions()”中,所以我认为这就是这里所做的,只是使用了不同的名称。对于这个推荐的函数,我仍然会为每个添加的事务获取一个新的列表。最终,我试图在同一个列表中添加其他值。我的好人!你做到了,非常感谢!