0

I know, the title to this question is vague, but I don't really know how else to describe it. Here's what I'm trying to do though.

I'm using pyodbc to query a database, and I want to allow the user to specify a column, and for my code to fetch it dynamically. In other words, I need to do something like this:

row_id = "row.%s" % id_column

Of course, this returns a string. What pyodbc is expecting is a variable/property, like so:

row_id = row.id

Could someone please tell me how to do this? I tried searching for "python string to val" but all those pages tell me how to convert a string to an int- which is different than what I'm trying to do.

1

2 Answers 2

6

You are looking for getattr:

row_id = getattr(row, id_column)
Sign up to request clarification or add additional context in comments.

Comments

1

You mean something like:

getattr(row,'id')

or maybe I don't really get what you want to do...

Comments

Your Answer

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.