处理JsonFi

2024-09-30 10:37:47 发布

您现在位置:Python中文网/ 问答频道 /正文

Commercial data processing - StockAccount.py implements a data type that might be used by a financial institution to keep track of customer information. The StockAccount class implements following methods The StockAccount class also maintains a list of CompanyShares object which has Stock Symbol and Number of Shares as well as DateTime of the transaction. When buy or sell is initiated StockAccount checks if CompanyShares are available and accordingly update or create an Object.

Actual Code that I have tried

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
        }
    ]
}

有人能帮我吗?我在上面描述的问题


Tags: nameinforinputagevaluecountopen

热门问题