Click here to Skip to main content
15,888,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to call a api from the output of a query , the output of the query is int , but when i call the query it returns something like [(12345,)], but i want only 12345 how to reconstruct the output

What I have tried:

query = "select some query limit 1"
   print (query)
   cursor.execute(query)
   contact = cursor.fetchall()
   print(contact)
   id = re.sub(r"[(,)]",r"",contact)
   print (111111111111111) --this is just a point to check the output
   print(id)
Posted
Updated 13-Oct-20 21:50pm

1 solution

Based on what you share, it seems to be a valid output of your code. Like an array. You can always access them using index, try that.

If you are still looking for replacement/cleanup option, there would be various ways to do it. If your numbers are separated by ',' always then use split/replace:
Python
# str is string input that would be converted to int in the end
# with split get them separated
[int(n) for n in str.split(',') if n.isdigit()] 

# with replace, remove it
int(str.replace(',', ''))

If you want, you can always use Regular expressions though[^]:
Python
import rex
rex.sub("[0-9]+", "", str)
#OR
rex.sub(r',', "", str)
#OR
rex.sub(r'\D', "", str) 

You can also use join/filter/translate type operations to clean up your string.

Use based on the situation and desired effect you need.
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900