Advertisements
I am trying to loop through a list of tupples, which I want to converto to a list of integers.
My function get_ids returns a list of tupples like so: [(8720581,), (8755416,),…]
def send_to_MQ():
credentials = pika.PlainCredentials(rabbit_username,
rabbit_password)
connection=pika.BlockingConnection(pika.ConnectionParameters(rabbit_server,
rabbit_port,"/", credentials ))
channel = connection.channel()
channel.queue_declare(queue=rabbit_queue, durable=True)
mapped_ids=get_mapped_ids()
for mapped_id in mapped_ids:
mapped_id=int(mapped_id)
channel.basic_publish(exchange='', routing_key='hhh', body=mapped_id)
print("Mapped ids sent to RabbitMQ")
When I ran the code above, I got: TypeError: sequence item 0: expected a bytes-like object, tuple found and I tried the following solutions:
for mapped_id in mapped_ids:
mapped_id=int(mapped_id[0])
but I have this error: TypeError: object of type ‘int’ has no len()
and
for mapped_id in mapped_ids:
mapped_id=int(mapped_id)
TypeError: int() argument must be a string, a bytes-like object or a real number, not ‘tuple’
>Solution :
Your second and third sample are identical. Both should have worked fine, though. Yet another way to accomplish this could be
mapped_ids = get_ids()
for mapped_id, in mapped_ids:
channel.basic_publish(exchange='', routing_key='hhhh', body=int(mapped_id))
However, please check if channel.basic_publish
actually expects an integer as its body=
argument; I would have expected it wants a string.