2017-07-07 13 views
0

はそれからvalue部分を取得するためにどのようにタイプのdict処理Pythonの辞書

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

です。 私はたくさん試しましたが、私はそれをすることができません。提案してください。

私はこれが欲しいです。

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

を知りたいですか?値はリストに含まれているようです。 –

+0

キーエラー 'print(message [0] ['value'])KeyError:0 – Avi

+1

これらの' TopicPartition'クラスと 'ConsumerRecord'クラスが何であるか分からず、言うことは難しいです。しかし、あなたは 'next(iter(message.values()))[0] .value'を試すことができます。 –

答えて

2

これは名前のタプル

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

他のユーザーのための完全なコードと呼ばれているあなたは、 `メッセージ[0] [ '値を']`てみました

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