0

I've sucessfully done a Select query on Python using psycopg2, but i have a problem with the output.

The output is as follows:

[('Porto',), ('Milan',), ('London',)]

The problem is when i try to pass it as a variable on an API string

cityList = records
for city in cityList:
 api = f"https://api.example.io/?city={city}"

I get an API error because the input it's been read as:

https://api.example.io/?city=('Porto',)

I know it's because it's a string, but how to i pass it to the API as a variable in a way that i can loop through the DB results as:

https://api.example.io/?city=Porto

Thank you for any help.

jarlh
44.9k8 gold badges52 silver badges68 bronze badges
asked Oct 26, 2022 at 18:55

1 Answer 1

1

You have a list of rows where each element is a tuple containing all the columns retrieved for each row, which in your case contains a single element. So what you should do instead is chose the one and only column from each row:

cityList = records
for city in cityList:
 api = f"https://api.example.io/?city={city[0]}"

What you were doing was converting the entire tuple to a string instead of the first and only element within the tuple.

answered Oct 26, 2022 at 19:06
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.