从JSON检索数据

时间:2019-06-19 18:55:32

标签: python json

我有3个记录销售情况的json文件,我需要检索每台机器记录的销售总量并在使用类时打印出其百分比和总销售量。

简短的Json文件信息。我需要根据“所有库存”的“最后库存”和“当前库存”以及总销售额来确定百分比。每个json文件中有多个项目我需要汇总在一起。

    ,,,machine 1:,,,
     "contents": [
            {
                "row": "A",
                "slots": [
                    {
                        "current_stock": 0,
                        "item_name": "Coke",
                        "item_price": 1.75,
                        "last_stock": 5,
                        "slot_number": 1
    {
                        "current_stock": 2,
                        "item_name": "Monster Energy Zero",
                        "item_price": 1.75,
                        "last_stock": 2,
                        "slot_number": 8
                    },
     "machine_id": "X2077070923",
        "machine_label": "REID_1F"

    I have tried working many different ways but I think my classes are screwy.

    ####Here is the code I have so far:###










    ,,,,,,class Machine:
        def __init__(self,machineName):
            self.machinename = machineName
            self.CurrentMachinestock =0
            self.MachinOriginalStock = 0
            self.TotalMachineSlots = 0

        def GetMachineSoldPct (self):
            return self.MachinOriginalStock - self.CurrentMachinestock/self.MachinOriginalStock

        def GetTotalMachineSales (self):
            return self.MachinOriginalStock - self.CurrentMachinestock * self.MachinOriginalStock

        def MachineIncrementSlots( self ):
            self.TotalMachineSlots = self.TotalMachineSlots + 1

    ######Json Files#####
    #def main():
        inventoryFileNames = ["REID_1F_20171004.json", "REID_2F_20171004.json", "REID_3F_20171004.json"]

    ####Attempt to tally Machines but not working ######

while sortChoice!='q':

    MachineSTock = input('Would you like the (m) machine report or the (i)inventory report?:')


     if MachineSTock == 'm':
                print('Label              Pct Sold              Sales ' )  
                for inventoryFileName in inventoryFileNames:
                    inventoryFile = open( inventoryFileName, 'r' )
                    inventoryData = json.loads( inventoryFile.read() )

                    ######Im almost Positive this is the issues#######
                    for row in contents:
                         for slot in row['slots']:
                             MachineMoney = slot['item_price']
                             MachineinventoryItem = itemNameToInventoryItem.get (MachineMoney,Machine(MachineMoney))
                             MachineinventoryItem.MachinOriginalStock( slot['last_stock'] )
                             MachineinventoryItem.CurrentMachinestock( slot['current_stock'] )
                             MachineinventoryItem.MachineIncrementSlots()

       print (inventoryData['machine_label'], ":")
在我添加问题区域之前,它会打印出每台计算机的名称。我需要它来汇总每台机器的所有价格,并根据每台机器的销售量来确定百分比。

回溯(最近通话最近): TypeError:'int'对象不可调用######

0 个答案:

没有答案