Python 将数据帧数据附加到json文件而不删除以前的内容

Python 将数据帧数据附加到json文件而不删除以前的内容,python,json,pandas,Python,Json,Pandas,让我先说我是一个新手,这段代码可能很难看。 我试图将dataframe数据附加到json文件中,而不在每次后续运行时删除json数据的先前内容 import json import pandas as pd import datetime json_backup = 'temp.json' df_store = pd.DataFrame(columns=["Time", "Average Rate"]) while True: #doing some data gathering (c

让我先说我是一个新手,这段代码可能很难看。 我试图将dataframe数据附加到json文件中,而不在每次后续运行时删除json数据的先前内容

import json
import pandas as pd
import datetime

json_backup = 'temp.json'
df_store = pd.DataFrame(columns=["Time", "Average Rate"])

while True:
   #doing some data gathering (code not included here) at each loop
   df_store = df_store.append({
             "Time": datetime.datetime.now(),
            "Average Rate": average_rate
             }, ignore_index=True)
  df_store.to_json(json_backup)

backup = pd.read_json(json_backup)
print (backup)
因此,在我重新启动脚本并删除json数据之前,所有新数据都会添加到json中。
我应该如何继续,以便保留这些数据,并将所有新数据添加到json文件中?

我认为,如果
'temp.json'
文件存在,则应该在
开始之前阅读
'temp.json'
循环到
df_store
变量中

@Tamis,请尝试下面的代码
只需复制代码(Python3),粘贴,运行

注意:我将平均速率的初始值作为日期时间的微秒部分。现在()只是为了测试代码。您可以根据您的逻辑计算其值,因此请更改它

您不需要创建任何temp.json文件。它将自动创建

只需将append_data_的内容复制到_json-v2-py3.py文件中,粘贴然后运行

每次更新temp.json后,按Y/Y继续,否则按任何其他键退出while循环

»将_数据_附加到_json-v2-py3.py »首次运行 »第二次运行: »第三次运行
就我所知,你不能。一种解决方法是加载当前文件(
df_old=pd.read_json(json_backup)
),然后连接两个数据帧(可能与
df_old.append(df_store)
类似),最后保存连接的数据帧(
df_old.to_json(json_backup)
)也许你也可以看看:谢谢Kostas Mouratidis,你是对的,这是不可能做到的。为什么会有这么多重复的代码和赤裸裸的异常?人们可能会从你的帖子中学到这一点!@Evgeny,我不明白,你说的是代码的哪一部分。重复的代码?大部分代码都在尝试中,除非你注意到了,那就是correct。我们可以设计一个代码块并将其放在其中,但它只有3-4行代码,所以我认为,不要用2个函数调用使问题变得更复杂。因此,完全取决于程序员如何设计代码。使用函数来消除代码重复很好。谢谢。最好做出好的编程决策,对吗?是的异常?也许您知道可能会发生什么样的错误?
import json
import pandas as pd
import datetime
import time # {Added}

json_backup = 'temp.json'
df_store = pd.DataFrame(columns=["Time", "Average Rate"])

average_rate = 50  # {Added}
count = 1          # {Added}

while True:
    # Doing some data gathering (code not included here) at each loop
    time_data = str(datetime.datetime.now())

    # Store micro seconds of datetime as average 
    # (For test only, use your own logic to calculate it)
    # In case of 2018-06-03 18:44:56.220778 => 220778
    average_rate = int((time_data.split()[1]).split('.')[1]) 

    try:
        df_store = pd.read_json(json_backup)

        df_store = df_store.append({
            "Time": time_data,
            "Average Rate": average_rate
        }, ignore_index=True)
        
        df_store.to_json(json_backup)
        print (df_store)
        print ("***********************************************")       
    except Exception as e:
        df_store = df_store.append({
            "Time": time_data,
            "Average Rate": average_rate
            }, ignore_index=True)

        df_store.to_json(json_backup)
        print(df_store)
        print("***********************************************")

    # time.sleep(30.0 - ((time.time() - starttime) % 30.0))  # {Commented}
    print(count, "temp.json updated")

    # If user presses Y/y then continue otherwise exit from loop
    choice = input("\n" + str(count) + " Do you want to continue the operation (Y/N): ")
    if choice == 'y' or choice == 'Y':
        count = count + 1
        continue
    else:
        break
(py3.6) H:\RishikeshAgrawani\Projects\Python3\Pandas>python append_data_to_json-v2-py3.py
                         Time Average Rate
0  2018-06-03 18:51:57.506959       506959
***********************************************
1 temp.json updated

1 Do you want to continue the operation (Y/N): y
                         Time  Average Rate
0  2018-06-03 18:51:57.506959        506959
1  2018-06-03 18:52:00.925554        925554
***********************************************
2 temp.json updated

2 Do you want to continue the operation (Y/N): y
                         Time  Average Rate
0  2018-06-03 18:51:57.506959        506959
1  2018-06-03 18:52:00.925554        925554
2  2018-06-03 18:52:02.325613        325613
***********************************************
3 temp.json updated

3 Do you want to continue the operation (Y/N): y
                         Time  Average Rate
0  2018-06-03 18:51:57.506959        506959
1  2018-06-03 18:52:00.925554        925554
2  2018-06-03 18:52:02.325613        325613
3  2018-06-03 18:52:03.508673        508673
***********************************************
4 temp.json updated

4 Do you want to continue the operation (Y/N): y
                         Time  Average Rate
0  2018-06-03 18:51:57.506959        506959
1  2018-06-03 18:52:00.925554        925554
2  2018-06-03 18:52:02.325613        325613
3  2018-06-03 18:52:03.508673        508673
4  2018-06-03 18:52:07.772553        772553
***********************************************
5 temp.json updated

5 Do you want to continue the operation (Y/N): n
(py3.6) H:\RishikeshAgrawani\Projects\Python3\Pandas>python append_data_to_json-v2-py3.py
                       Time  Average Rate
0  2018-06-03 18:51:57.506959        506959
1  2018-06-03 18:52:00.925554        925554
2  2018-06-03 18:52:02.325613        325613
3  2018-06-03 18:52:03.508673        508673
4  2018-06-03 18:52:07.772553        772553
5  2018-06-03 18:53:52.484954        484954
***********************************************
1 temp.json updated

1 Do you want to continue the operation (Y/N): y
                       Time  Average Rate
0  2018-06-03 18:51:57.506959        506959
1  2018-06-03 18:52:00.925554        925554
2  2018-06-03 18:52:02.325613        325613
3  2018-06-03 18:52:03.508673        508673
4  2018-06-03 18:52:07.772553        772553
5  2018-06-03 18:53:52.484954        484954
6  2018-06-03 18:53:54.733274        733274
***********************************************
2 temp.json updated

2 Do you want to continue the operation (Y/N): y
                       Time  Average Rate
0  2018-06-03 18:51:57.506959        506959
1  2018-06-03 18:52:00.925554        925554
2  2018-06-03 18:52:02.325613        325613
3  2018-06-03 18:52:03.508673        508673
4  2018-06-03 18:52:07.772553        772553
5  2018-06-03 18:53:52.484954        484954
6  2018-06-03 18:53:54.733274        733274
7  2018-06-03 18:53:57.037358         37358
***********************************************
3 temp.json updated

3 Do you want to continue the operation (Y/N): y
                       Time  Average Rate
0  2018-06-03 18:51:57.506959        506959
1  2018-06-03 18:52:00.925554        925554
2  2018-06-03 18:52:02.325613        325613
3  2018-06-03 18:52:03.508673        508673
4  2018-06-03 18:52:07.772553        772553
5  2018-06-03 18:53:52.484954        484954
6  2018-06-03 18:53:54.733274        733274
7  2018-06-03 18:53:57.037358         37358
8  2018-06-03 18:53:58.437644        437644
***********************************************
4 temp.json updated

4 Do you want to continue the operation (Y/N): y
                       Time  Average Rate
0  2018-06-03 18:51:57.506959        506959
1  2018-06-03 18:52:00.925554        925554
2  2018-06-03 18:52:02.325613        325613
3  2018-06-03 18:52:03.508673        508673
4  2018-06-03 18:52:07.772553        772553
5  2018-06-03 18:53:52.484954        484954
6  2018-06-03 18:53:54.733274        733274
7  2018-06-03 18:53:57.037358         37358
8  2018-06-03 18:53:58.437644        437644
9  2018-06-03 18:53:59.181472        181472
***********************************************
5 temp.json updated

5 Do you want to continue the operation (Y/N): y
                        Time  Average Rate
0   2018-06-03 18:51:57.506959        506959
1   2018-06-03 18:52:00.925554        925554
2   2018-06-03 18:52:02.325613        325613
3   2018-06-03 18:52:03.508673        508673
4   2018-06-03 18:52:07.772553        772553
5   2018-06-03 18:53:52.484954        484954
6   2018-06-03 18:53:54.733274        733274
7   2018-06-03 18:53:57.037358         37358
8   2018-06-03 18:53:58.437644        437644
9   2018-06-03 18:53:59.181472        181472
10  2018-06-03 18:53:59.805276        805276
***********************************************
6 temp.json updated

6 Do you want to continue the operation (Y/N): y
                        Time  Average Rate
0   2018-06-03 18:51:57.506959        506959
1   2018-06-03 18:52:00.925554        925554
2   2018-06-03 18:53:59.805276        805276
3   2018-06-03 18:52:02.325613        325613
4   2018-06-03 18:52:03.508673        508673
5   2018-06-03 18:52:07.772553        772553
6   2018-06-03 18:53:52.484954        484954
7   2018-06-03 18:53:54.733274        733274
8   2018-06-03 18:53:57.037358         37358
9   2018-06-03 18:53:58.437644        437644
10  2018-06-03 18:53:59.181472        181472
11  2018-06-03 18:54:00.436774        436774
***********************************************
7 temp.json updated

7 Do you want to continue the operation (Y/N): y
                        Time  Average Rate
0   2018-06-03 18:51:57.506959        506959
1   2018-06-03 18:52:00.925554        925554
2   2018-06-03 18:53:59.181472        181472
3   2018-06-03 18:54:00.436774        436774
4   2018-06-03 18:53:59.805276        805276
5   2018-06-03 18:52:02.325613        325613
6   2018-06-03 18:52:03.508673        508673
7   2018-06-03 18:52:07.772553        772553
8   2018-06-03 18:53:52.484954        484954
9   2018-06-03 18:53:54.733274        733274
10  2018-06-03 18:53:57.037358         37358
11  2018-06-03 18:53:58.437644        437644
12  2018-06-03 18:54:00.997659        997659
***********************************************
8 temp.json updated

8 Do you want to continue the operation (Y/N): y
                        Time  Average Rate
0   2018-06-03 18:51:57.506959        506959
1   2018-06-03 18:52:00.925554        925554
2   2018-06-03 18:53:57.037358         37358
3   2018-06-03 18:53:58.437644        437644
4   2018-06-03 18:54:00.997659        997659
5   2018-06-03 18:53:59.181472        181472
6   2018-06-03 18:54:00.436774        436774
7   2018-06-03 18:53:59.805276        805276
8   2018-06-03 18:52:02.325613        325613
9   2018-06-03 18:52:03.508673        508673
10  2018-06-03 18:52:07.772553        772553
11  2018-06-03 18:53:52.484954        484954
12  2018-06-03 18:53:54.733274        733274
13  2018-06-03 18:54:01.549498        549498
***********************************************
9 temp.json updated

9 Do you want to continue the operation (Y/N): y
                        Time  Average Rate
0   2018-06-03 18:51:57.506959        506959
1   2018-06-03 18:52:00.925554        925554
2   2018-06-03 18:52:07.772553        772553
3   2018-06-03 18:53:52.484954        484954
4   2018-06-03 18:53:54.733274        733274
5   2018-06-03 18:54:01.549498        549498
6   2018-06-03 18:53:57.037358         37358
7   2018-06-03 18:53:58.437644        437644
8   2018-06-03 18:54:00.997659        997659
9   2018-06-03 18:53:59.181472        181472
10  2018-06-03 18:54:00.436774        436774
11  2018-06-03 18:53:59.805276        805276
12  2018-06-03 18:52:02.325613        325613
13  2018-06-03 18:52:03.508673        508673
14  2018-06-03 18:54:02.061568         61568
***********************************************
10 temp.json updated

10 Do you want to continue the operation (Y/N): y
                        Time  Average Rate
0   2018-06-03 18:51:57.506959        506959
1   2018-06-03 18:52:00.925554        925554
2   2018-06-03 18:54:00.436774        436774
3   2018-06-03 18:53:59.805276        805276
4   2018-06-03 18:52:02.325613        325613
5   2018-06-03 18:52:03.508673        508673
6   2018-06-03 18:54:02.061568         61568
7   2018-06-03 18:52:07.772553        772553
8   2018-06-03 18:53:52.484954        484954
9   2018-06-03 18:53:54.733274        733274
10  2018-06-03 18:54:01.549498        549498
11  2018-06-03 18:53:57.037358         37358
12  2018-06-03 18:53:58.437644        437644
13  2018-06-03 18:54:00.997659        997659
14  2018-06-03 18:53:59.181472        181472
15  2018-06-03 18:54:03.420695        420695
***********************************************
11 temp.json updated

11 Do you want to continue the operation (Y/N): n
(py3.6) H:\RishikeshAgrawani\Projects\Python3\Pandas>python append_data_to_json-v2-py3.py
                          Time  Average Rate
0   2018-06-03 18:51:57.506959        506959
1   2018-06-03 18:52:00.925554        925554
2   2018-06-03 18:54:01.549498        549498
3   2018-06-03 18:53:57.037358         37358
4   2018-06-03 18:53:58.437644        437644
5   2018-06-03 18:54:00.997659        997659
6   2018-06-03 18:53:59.181472        181472
7   2018-06-03 18:54:03.420695        420695
8   2018-06-03 18:54:00.436774        436774
9   2018-06-03 18:53:59.805276        805276
10  2018-06-03 18:52:02.325613        325613
11  2018-06-03 18:52:03.508673        508673
12  2018-06-03 18:54:02.061568         61568
13  2018-06-03 18:52:07.772553        772553
14  2018-06-03 18:53:52.484954        484954
15  2018-06-03 18:53:54.733274        733274
16  2018-06-03 18:55:39.415698        415698
***********************************************
1 temp.json updated

1 Do you want to continue the operation (Y/N): y
                          Time  Average Rate
0   2018-06-03 18:51:57.506959        506959
1   2018-06-03 18:52:00.925554        925554
2   2018-06-03 18:52:02.325613        325613
3   2018-06-03 18:52:03.508673        508673
4   2018-06-03 18:54:02.061568         61568
5   2018-06-03 18:52:07.772553        772553
6   2018-06-03 18:53:52.484954        484954
7   2018-06-03 18:53:54.733274        733274
8   2018-06-03 18:55:39.415698        415698
9   2018-06-03 18:54:01.549498        549498
10  2018-06-03 18:53:57.037358         37358
11  2018-06-03 18:53:58.437644        437644
12  2018-06-03 18:54:00.997659        997659
13  2018-06-03 18:53:59.181472        181472
14  2018-06-03 18:54:03.420695        420695
15  2018-06-03 18:54:00.436774        436774
16  2018-06-03 18:53:59.805276        805276
17  2018-06-03 18:55:41.861641        861641
***********************************************
2 temp.json updated

2 Do you want to continue the operation (Y/N): y
                          Time  Average Rate
0   2018-06-03 18:51:57.506959        506959
1   2018-06-03 18:52:00.925554        925554
2   2018-06-03 18:53:57.037358         37358
3   2018-06-03 18:53:58.437644        437644
4   2018-06-03 18:54:00.997659        997659
5   2018-06-03 18:53:59.181472        181472
6   2018-06-03 18:54:03.420695        420695
7   2018-06-03 18:54:00.436774        436774
8   2018-06-03 18:53:59.805276        805276
9   2018-06-03 18:55:41.861641        861641
10  2018-06-03 18:52:02.325613        325613
11  2018-06-03 18:52:03.508673        508673
12  2018-06-03 18:54:02.061568         61568
13  2018-06-03 18:52:07.772553        772553
14  2018-06-03 18:53:52.484954        484954
15  2018-06-03 18:53:54.733274        733274
16  2018-06-03 18:55:39.415698        415698
17  2018-06-03 18:54:01.549498        549498
18  2018-06-03 18:55:44.381318        381318
***********************************************
3 temp.json updated

3 Do you want to continue the operation (Y/N): y
                          Time  Average Rate
0   2018-06-03 18:51:57.506959        506959
1   2018-06-03 18:52:00.925554        925554
2   2018-06-03 18:52:02.325613        325613
3   2018-06-03 18:52:03.508673        508673
4   2018-06-03 18:54:02.061568         61568
5   2018-06-03 18:52:07.772553        772553
6   2018-06-03 18:53:52.484954        484954
7   2018-06-03 18:53:54.733274        733274
8   2018-06-03 18:55:39.415698        415698
9   2018-06-03 18:54:01.549498        549498
10  2018-06-03 18:55:44.381318        381318
11  2018-06-03 18:53:57.037358         37358
12  2018-06-03 18:53:58.437644        437644
13  2018-06-03 18:54:00.997659        997659
14  2018-06-03 18:53:59.181472        181472
15  2018-06-03 18:54:03.420695        420695
16  2018-06-03 18:54:00.436774        436774
17  2018-06-03 18:53:59.805276        805276
18  2018-06-03 18:55:41.861641        861641
19  2018-06-03 18:55:45.181318        181318
***********************************************
4 temp.json updated

4 Do you want to continue the operation (Y/N): y
                          Time  Average Rate
0   2018-06-03 18:51:57.506959        506959
1   2018-06-03 18:52:00.925554        925554
2   2018-06-03 18:55:44.381318        381318
3   2018-06-03 18:53:57.037358         37358
4   2018-06-03 18:53:58.437644        437644
5   2018-06-03 18:54:00.997659        997659
6   2018-06-03 18:53:59.181472        181472
7   2018-06-03 18:54:03.420695        420695
8   2018-06-03 18:54:00.436774        436774
9   2018-06-03 18:53:59.805276        805276
10  2018-06-03 18:55:41.861641        861641
11  2018-06-03 18:55:45.181318        181318
12  2018-06-03 18:52:02.325613        325613
13  2018-06-03 18:52:03.508673        508673
14  2018-06-03 18:54:02.061568         61568
15  2018-06-03 18:52:07.772553        772553
16  2018-06-03 18:53:52.484954        484954
17  2018-06-03 18:53:54.733274        733274
18  2018-06-03 18:55:39.415698        415698
19  2018-06-03 18:54:01.549498        549498
20  2018-06-03 18:55:45.765547        765547
***********************************************
5 temp.json updated

5 Do you want to continue the operation (Y/N): n