处理JsonFile

时间:2019-10-23 07:11:49

标签: python json

  

商业数据处理-StockAccount.py实现了一种数据类型   金融机构可能会用来跟踪   客户资料。 StockAccount类实现以下   方法StockAccount类还维护一个CompanyShares列表   对象,其具有股票代码和股份数以及日期时间   交易。发起买卖时,StockAccount检查   如果CompanyShares可用,并相应地更新或创建   对象。

     

我尝试过的实际代码

import json
class StockAccount:


    def fileInput(self,filename):

        file = open(filename, "r")
        item = json.load(file)
        file.close()
        print("-------------------- Stock Data --------------------")
        for key in item:
            print("\n ------- ", key, " ------- \n")

            for value in item[key]:

                amount = count = 0

                print("Name :", value['name'])
                print("Age :", value['Age'])
                print("Dob :", value['Dob'])
                print("Address :", value['Address'])


                print("Share :", value['share'])
                print("Amount:", value['amount'])
                print("Stock:", value['stock'])
                print("Symbol:", value['symbol'])


                print("Product:", value['product'])
                print("Count:", value['count'])
                print("ProductionDate:", value['productionDate'])
                print("Investment:", value['investment'])

                amount += int(value["amount"])
                count += int(value["count"])

                print("Ampount : ", amount)
                print("Count : ", count)

                total = int(amount) * int(count)

                print("Total Amount: ", total)
                value['total'] = total
                print(value)

                ls = list()
                ls.append(value)

                print("\n------------------------------\n")
                f = open("stockAccountData.json", "a+")
                f.write(str(ls)+",")
                print("Data is Written into 'stockAccountData.json' Successfully")
                print()



        #StockAccount.valueOf(amount)
        #print(cont)




    def valueOf(self):
        f = open(filename,"r+")
        text = list()
        for i in f:
            text.append(f.readlines())
        print(text)
        #amount = int(input("Enter the amount that you want to spend on shares: ")
        #add = text.append(amount)



    def buy(amount,symbol):
        f = open(filename,"r+")
        cont = list()
        for i in f:
            cont = f.readlines()
        print(cont)


    def sell(amount,symbol):
        f = open(filename,"r+")
        cont = list()
        for i in f:
            cont = f.readlines()
        print(cont)



    def save(filename):
        f = open("StockA.txt,"w+")
        cont = list()
        for i in f:
            cont = f.readlines()
        print(cont)


    def displayReport():
        f = open(filename,"w+")
        cont = list()
        for i in f:
            cont = f.readlines()
        print(cont)


StAc = StockAccount()

if __name__ == '__main__' :

    inp = input(" Are having an Existing Account? (y/n):")
    if (inp == "y") or (inp == "Y"):
        filename = input("Enter the File name: ")
        StAc.fileInput(filename)
        StAc.valueOf()
    else:
        f = open("newUser.json","a+")
        name = input("Enter the Name: ")
        Age = int(input("Enter the Age: "))
        phno = int(input("Enter your Mobile Number: "))
        amount = int(input("Enter the Amount for Shares: "))
        cont = f.write(' [{'+'     "Name" : "'+ name +'",\n' + '        "Age"  : "'+str(Age)+'",\n'+'       "Ph.No" : "'+str(phno)+'",\n'+'"Share Amount" : "'+str(amount)+'" }]')
        print("The Data has been Stored Successfully in 'newUser.json' file. ")

Json文件

{
    "Users" : [{
        "name":"Mohammed Hussain Sab",
        "Age": 22,
        "Dob": "28/05/1997",
        "Address": "Ilyas Nagar Shimogga"
        },
        {
        "name":"Venkateshwara G",
        "Age": 23,
        "Dob": "15/10/1996",
        "Address": "Basavanagudi Shimogga"
        },
        {
        "name":"Sabiha Banu",
        "Age": 23,
        "Dob": "28/03/1996",
        "Address": "Ilyas Nagar Shimogga"
        }
    ],
    "Shares" : [
        {
        "share":"Production",
        "amount": 25000,
        "stock": 10000,
        "symbol": "$"
        },
        {
        "share":"Sales",
        "amount": 500000,
        "stock": 14000,
        "symbol": "$"
        },
        {
        "share":"Marketing",
        "amount": 240000,
        "stock": 15000,
        "symbol": "$"
        }
    ],

    "Products" : [{ 
        "product":"Audi",     
        "count": 2400,
        "productionDate": "20/05/2019",
        "investment": 5000000
        },
        {   
        "product":"Benz",   
        "count": 22,
        "Dob": 170000,
        "investment": 2500000
        },
        {    
        "product":"Ford",  
        "count": 18,
        "productionDate": 1500000,
        "investment": 50000000
        }
    ]
}

有人可以帮我吗?我上面已经描述了这个问题。

0 个答案:

没有答案