2017-07-07 8 views
0

Ich habe eine Variable message vom Typ dictVerarbeitung Python Wörterbuch

>>> print(message) 
{TopicPartition(topic='testing_topic', partition=1): [ConsumerRecord(topic='testing_topic', partition=1, offset=0, timestamp=1499411365748, timestamp_type=0, key=None, value=b'{"url":"http://review.travel.rakuten.co.jp/hotel/voice/158886/", "meta":{"url_cnt":"1185","hotelid":"158886","job_id":"0f3de6d4-34bf-4a1a-a803-5640e8d25932"}}', checksum=-309359221, serialized_key_size=-1, serialized_value_size=158)]} 

Wie value Teil von ihm zu holen. Ich habe viel versucht, aber ich kann es nicht tun. Bitte vorschlagen.

Ich möchte das.

value=b'{"url":"http://review.travel.rakuten.co.jp/hotel/voice/158886/", "meta":{"url_cnt":"1185","hotelid":"158886","job_id":"0f3de6d4-34bf-4a1a-a803-5640e8d25932"}}' 
+0

Haben Sie versucht, 'message [0] [ 'value']'? Es scheint, dass die Werte in einer Liste enthalten sind. –

+0

Ich bekomme Schlüsselfehler 'print (Nachricht [0] ['Wert'])' KeyError: 0 – Avi

+1

Ohne zu wissen, was diese 'TopicPartition' und' ConsumerRecord' Klassen sind, ist es schwer zu sagen. Aber Sie können 'next (iter (message.values ​​())) [0] .value' versuchen. –

Antwort

2

wird dieses genannt Tupel genannt

for i in message.values(): 
...  for j in i: 
...  print(j.value) 

vollständigen Code für einen anderen Benutzer wollen wissen,

from collections import namedtuple 

R = namedtuple('TopicPartition','topic partition') 
index = R(topic='testing_topic', partition=1) 

V = namedtuple('ConsumerRecord','topic partition offset timestamp timestamp_ 
type key value checksum serialized_key_size serialized_value_size') 

content = V(topic='testing_topic', partition=1, offset=0, timestamp=14994113 
65748, timestamp_type=0, key=None, value=b'{"url":"http://review.travel.rakuten. 
co.jp/hotel/voice/158886/", "meta":{"url_cnt":"1185","hotelid":"158886","job_id" 
:"0f3de6d4-34bf-4a1a-a803-5640e8d25932"}}', checksum=-309359221, serialized_key_ 
size=-1, serialized_value_size=158) 

D = {index:[content]} 

for i in D.values(): 
...  for j in i: 
...  print(j.value) 

b'{"url":"http://review.travel.rakuten.co.jp/hotel/voice/158886/", "meta":{"url_ 
cnt":"1185","hotelid":"158886","job_id":"0f3de6d4-34bf-4a1a-a803-5640e8d25932"}}