How do I replace a column name with a variable to get a row?

What I’m trying to do:
I wrote a function on the server side that gets the following parameters from the user side:
a selected column name in a variable called colName and
a specific text from that column, here called foundText
I try to replace a real column name here sl1Name with the variable colName to get a row, but the real column name can be sl2Name , sl3Name … depending on the user choice.

What I’ve tried and what’s not working:

Code Sample:

# Example :
rowSel = app_tables.tp_table.get(sl1Name=foundText)
# I want to replace the real column name sl1Name with the variable colName to get:
#  rowSel = app_tables.tp_table.get(colName=foundText)
# even with rowSel = app_tables.tp_table.get(f’{colName}’=foundText)
# both lines above give errors


How do I do that?

Unpack the dictionary so it becomes keyword arguments:
**{colName: "some value"}

And pop that into your get method.

1 Like

Ok thanks I will try to work this out and will let it know.
I thought more at:


rowSel = app_tables.tp_table.get(**{‘data_key’: colName}=foundText)
# or
rowSel = app_tables.tp_table.get(**{‘name’: colName}=foundText)

Those options are definite syntax errors,

The first option is an error because the only way you can pass a string/variable as the keyword argument name is by unpacking a dictionary.

The second option is an error because you are both unpacking a dictionary for keyword args and using the equals expression altogether, you have to choose one or the other.

OK thanks a lot for this info. :slight_smile:
I try to replace a real column name here sl1Name with the variable
colName to get a row, but the real column name can be sl2Name, sl3Name … depending on the user choice.
But in my example the variable colName stands for a value and not a key
So what must I put in your example “some value”?

**{colName : “some value”}

1 Like

The keys in the dictionary are the column names in string form, the dictionary values are whatever value you would normally put after the equals sign.

2 Likes