简体   繁体   English

从字典中读取时,我得到“int”对象没有属性“__getitem__”错误

[英]When reading from dictionary I get 'int' object has no attribute '__getitem__' error

In one python file I store values in a dictionary eg:在一个 python 文件中,我将值存储在字典中,例如:

 messageDict[ObservationRawDataId] = {'timestamp': TimeStamp, 'tanksystemid': TankSystemId,
                        'newDelivery': delivery,'oldDelivery':RawDeliveryLitres ,'grade' :ProductName,'sitecode':SiteCode}
 formMessageBody(messageDict)

In another python file I try to formulate a plain text messge from that dictionary;在另一个 python 文件中,我尝试从该字典中制定纯文本消息;

def formMessageBody( messageDict) :
    try :
        print 'SiteCode', 'Date', 'Product', 'NewDelievry','OldDelivery'
        for tuple_a in messageDict.items():
            for dic_a in tuple_a:
                print tuple_a
                print dic_a['sitecode']
                print  dic_a['sitecode'], dic_a['timestamp'], dic_a['grade'],
                                                            dic_a['newDelivery'],dic_a['oldDelivery']
    except Exception as error:
        print format(error)

but I could not read value from the tuple.但我无法从元组中读取值。 tuple_a prints as; tuple_a 打印为;

(14118912, {'newDelivery': '8397.000', 'grade': u'Unleaded', 'timestamp': datetime.datetime(2019, 6, 23, 0, 0), 'tanksystemid': 5977, 'oldDelivery': 8397.0, 'sitecode': u'1156'})

When I try to retrieve;当我尝试检索时; dic_a['sitecode'] dic_a['timestamp'] I get; dic_a['sitecode'] dic_a['timestamp'] 我明白了;

'int' object has no attribute '__getitem__'

error.错误。 What Im doing wrong here?我在这里做错了什么?

messageDict looks liek; messageDict 看起来很像;

{14090233: {'newDelivery': '5009.000', 'grade': u'E10', 'timestamp': datetime.datetime(2019, 6, 21, 0, 0), 'tanksystemid': 5776, 'oldDelivery': 5009.0, 'sitecode': u'4169'}, 14129146: {'newDelivery': '17091.000', 'grade': u'Unleaded', 'timestamp': datetime.datetime(2019, 6, 24, 0, 0), 'tanksystemid': 8720, 'oldDelivery': 17091.0, 'sitecode': u'2328'}, 14118907: {'newDelivery': '13797.000', 'grade': u'Unleaded', 'timestamp': datetime.datetime(2019, 6, 23, 0, 0), 'tanksystemid': 5973, 'oldDelivery': 13797.0, 'sitecode': u'1151'}, 14145533: {'newDelivery': '8281.000', 'grade': u'PULP', 'timestamp': datetime.datetime(2019, 6, 24, 0, 0), 'tanksystemid': 5360, 'oldDelivery': 8281.0, 'sitecode': u'2212'}, 14129150: {'newDelivery': '7099.000', 'grade': u'Diesel', 'timestamp': datetime.datetime(2019, 6, 24, 0, 0), 'tanksystemid': 8724, 'oldDelivery': 7099.0, 'sitecode': u'2328'}, 14129565: {'newDelivery': '16619.100', 'grade': u'Unleaded', 'timestamp': datetime.datetime(2019, 6, 24, 0, 0), 'tanksystemid': 10012, 'oldDelivery': 16619.1, 'sitecode': u'4217'}}

It looks like you only want to iterate the values of the dictionary.看起来您只想迭代字典的值。 If so, you can accomplish this by iterating through messageDict.values() .如果是这样,您可以通过迭代messageDict.values()来完成此操作。 This also removes the need for the inner for loop:这也消除了对内部 for 循环的需要:

for dic_a in messageDict.values():
    print dic_a['sitecode']
    print dic_a['sitecode'], dic_a['timestamp'], dic_a['grade'],
          dic_a['newDelivery'], dic_a['oldDelivery']

Other people have already pointed out the problem, I'll just try to explain it a little more as to why it is happening the way it is happening.其他人已经指出了这个问题,我将尝试更多地解释它为什么会以这种方式发生。

Your messageDict is a dictionary with the ObservationRawDataId as key and another dict as value.您的messageDict是一个字典,以ObservationRawDataId为键,另一个字典为值。 When you call messageDict.items() , it returns a tuple (ObservationRawDataId, {'timestamp': TimeStamp... , so the first value in the tuple is ObservationRawDataId and not the dic_a as you are expecting it. If you want to iterate over the values, you can do it as @Tomothy32 suggested:当您调用messageDict.items() ,它会返回一个元组(ObservationRawDataId, {'timestamp': TimeStamp... ,因此元组中的第一个值是ObservationRawDataId而不是您期望的dic_a 。如果您想迭代在这些值上,您可以按照@Tomothy32 的建议进行操作:

for dic_a in messageDict.values():
    print dic_a['sitecode']
    print dic_a['sitecode'], dic_a['timestamp'], dic_a['grade'],
          dic_a['newDelivery'], dic_a['oldDelivery']

Or if you want to use ObservationRawDataId somewhere in the loop too, you can use:或者,如果您也想在循环中的某处使用ObservationRawDataId ,您可以使用:

for rawDataId in messageDict.keys():
    dic_a = messageDict[rawDataId]
    print dic_a['sitecode']
    print dic_a['sitecode'], dic_a['timestamp'], dic_a['grade'],
          dic_a['newDelivery'], dic_a['oldDelivery']

When you iterate through the list returned by messageDict.items() , you get tuples of key-value pairs (in your case int-dict pairs).当您遍历messageDict.items()返回的列表时,您将获得键值对(在您的情况下为 int-dict 对)的元组。 It seems like you are expecting only the values to be iterated through.似乎您只期望迭代这些值。

As you mentioned when you print the tuple, you get正如你在打印元组时提到的,你得到

(14118912, {'newDelivery': '8397.000', 'grade': u'Unleaded', 'timestamp': datetime.datetime(2019, 6, 23, 0, 0), 'tanksystemid': 5977, 'oldDelivery': 8397.0, 'sitecode': u'1156'}) (14118912, {'newDelivery': '8397.000', 'grade': u'Unleaded', 'timestamp': datetime.datetime(2019, 6, 23, 0, 0), 'tanksystemid': 5977, 'oldDelivery': 8397.0,'站点代码':u'1156'})

Note the first object in the tuple.注意元组中的第一个对象。 So through the first iteration of the inner loop, the variable dic_a stores 14118912 and calling dic_a['sitecode'] is what causes an error as an int object of course does not support this (This must be the __getitem__ attribute error thrown).所以通过内循环的第一次迭代,变量dic_a存储了14118912并且调用dic_a['sitecode']是导致错误的原因,因为 int 对象当然不支持这个(这肯定是抛出的 __getitem__ 属性错误)。

What I think you wanted to do is call messageDict.values() which only returns a list of the values (in your case a list of dictionary objects) in the dictionary.我认为您想要做的是调用messageDict.values() ,它只返回字典中的值列表(在您的情况下是字典对象列表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM