将JSON文件读取到熊猫中进行分析

时间:2019-02-01 21:00:33

标签: python json pandas

我逐渐熟悉Python中的Classs / OOP,并且正在练习一个用于跟踪财务的基本程序。我已经将程序添加到JSON文件中并保存该文件。现在,我想将JSON文件读入数据框并对其执行一些聚合。那就是我被困住的地方。以下内容失败并显示:

json.decoder.JSONDecodeError: Extra data: line 7 column 2 (char 122)

JSON文件如下所示:

{
    "DATE": "2019-02-01 12:57:13.140724",
    "HSA": "600",
    "401K": "90",
    "ROTH": "900",
    "SAVINGS": "1000"
}{
    "DATE": "2019-02-01 12:57:26.995724",
    "HSA": "250",
    "401K": "90",
    "ROTH": "80",
    "SAVINGS": "900"
}

有什么想法吗?

import datetime
import json
import pandas as pd

class BankAccount:

    def __init__(self):
        self.accounts = ['HSA', '401K', 'ROTH', 'SAVINGS']
        self.records = {}
        self.now = datetime.datetime.now()



    def data_entry(self):
        for i in self.accounts:
            x = input('Enter the amount for {}:'.format(i))
            self.records['DATE'] = self.now
            self.records[i] = x


    def display(self):
        return self.records

    def savefile(self):
        with open('finance.json', 'a') as file:
            file.write(json.dumps(self.records, indent=4, sort_keys=True, default=str))
            file.close()

    def analyzedata(self):
        with open('finance.json', 'r') as f:
            obj = json.load(f)
            frame = pd.DataFrame(obj, columns=['401K', 'HSA', 'ROTH', 'SAVINGS', 'DATE'])
            print(frame)





s = BankAccount()
s.data_entry()
s.savefile()
s.analyzedata()

顺便说一句,BTW随时提供其他建议,说明为什么这样做是一种不好的方式,即使用字典或任何可能的方式。还在学习。谢谢

1 个答案:

答案 0 :(得分:1)

JSON数据表示为一个字典,而不是文件中的多个字典。这就是说,我建议JSON格式基本上是一种具有密钥'data'并包含record字典列表的字典。我还修复了几个namimg约定,我说过这些小事情可以使我的评论更容易理解。

from datetime import datetime
import json
import pandas as pd

class BankAccount:

    def __init__(self, filename='finance.json'):
        self.accounts = ['HSA', '401K', 'ROTH', 'SAVINGS']
        self.records = []
        self.filename = filename

        #load data upon initialization
        self.load_data()

    def load_data(self):
        with open(self.filename, 'r') as file:
            #you may want to do some error checking here
            data = json.load(file)
            self.records = data.get('data', [])

    def data_entry(self):

        #make a new record with current date
        record = {'DATE': datetime.now()}
        for account_name in self.accounts:
            account_data = int(input('Enter the amount for {}:'.format(account_name)))
            record[account_name] = account_data
        self.records.append(record)

        #You made a modification to the records
        #now save it to file
        self.save_data()

    def save_data(self):
        with open(self.filename, 'w') as file:
            #possibly some error checking here as seen fit
            file.write(json.dumps({'data': self.records}, default=str))

    def analyze_edata(self):
        with open(self.filename, 'r') as file:
            df = pd.DataFrame(self.records, columns=self.accounts+['DATE'])
            print(df)

s = BankAccount()
s.data_entry()
s.save_data()
s.analyze_data()

当跑步: *几次**

Enter the amount for HSA:250
Enter the amount for 401K:90
Enter the amount for ROTH:80
Enter the amount for SAVINGS:900
['HSA', '401K', 'ROTH', 'SAVINGS', 'DATE']
   HSA  401K  ROTH  SAVINGS                        DATE
0  600    90   900     1000  2019-02-01 22:05:06.110471
1  360   100   250      430  2019-02-01 22:06:10.649269
2  250    90    80      900  2019-02-01 22:07:04.176700

finance.json

{
    "data": [{
        "401K": 90,
        "SAVINGS": 1000,
        "ROTH": 900,
        "HSA": 600,
        "DATE": "2019-02-01 22:05:06.110471"
    }, {
        "401K": 100,
        "SAVINGS": 430,
        "ROTH": 250,
        "HSA": 360,
        "DATE": "2019-02-01 22:06:10.649269"
    }, {
        "401K": 90,
        "ROTH": 80,
        "SAVINGS": 900,
        "HSA": 250,
        "DATE": "2019-02-01 22:07:04.176700"
    }]
}