【问题标题】:Appending dataframe data to a json file without deleting previous content将数据帧数据附加到 json 文件而不删除以前的内容
【发布时间】:2018-11-12 21:15:15
【问题描述】:

首先让我说我非常新手,这段代码可能很难看。 我正在尝试将数据帧数据附加到 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 文件中?

【问题讨论】:

  • 据我所知,你不能。一种解决方法是加载当前文件(df_old = pd.read_json(json_backup)),然后连接两个数据帧(可能与df_old.append(df_store) 类似,最后保存连接的数据帧(df_old.to_json(json_backup))也许你也可以采取看看:stackoverflow.com/a/30230394/6655150
  • 感谢 Kostas Mouratidis,您说得对,这是不可能的。

标签: python json pandas


【解决方案1】:

@Tamis,请尝试以下代码Just copy the code(Python3), paste, run

注意:我将 average_rate 的初始值作为 datetime.now() 的微秒部分只是为了测试代码。你可以根据你的逻辑计算它的值,所以请改变它。

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

只需复制 append_data_to_json-v2-py3.py 文件的内容,粘贴然后运行。

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

» append_data_to_json-v2-py3.py

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

【讨论】:

  • 为什么会有这么多重复的代码和裸露的异常?人们实际上可能会从您的帖子中学到这样做!
  • @Evgeny,我没听懂,你说的是代码的哪一部分。
  • 重复的?大部分代码都在 try 和 except,如果你注意到了
  • 是的,没错。我们可以设计一个代码块并将其放入其中,但它只有 3-4 行代码,所以我想,不要让 2 个函数调用使问题变得更复杂。所以这完全取决于程序员他/她如何设计代码。使用函数删除代码重复很好。谢谢。
  • 最好做出好的编程决策,对吧?赤裸裸的例外?也许您知道可能会发生什么样的错误?
【解决方案2】:

如果'temp.json' 文件存在,我认为您应该在while 循环开始之前将'temp.json' 读入df_store 变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2018-07-21
    • 1970-01-01
    • 2019-03-02
    • 2023-04-10
    • 2022-11-29
    • 2012-10-11
    相关资源
    最近更新 更多