Avinash
Avinash

Reputation: 2191

Processing python dictionary

I have a variable message is of type dict

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

How to fetch value portion from it. I tried a lot but I am unable to do it. Please suggest.

I want this.

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

Upvotes: 0

Views: 350

Answers (1)

Sugumar Venkatesan
Sugumar Venkatesan

Reputation: 4028

this is called named tuple

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

full code for some other user want to know

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

Upvotes: 2

Related Questions