Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert a tupple into integer Python, error message: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'tuple'

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading