table.read_where - can condition field name be a variable?

12 views
Skip to first unread message

Ken Walker

unread,
Sep 28, 2019, 5:05:08 PM9/28/19
to pytables-users
I am working with HDF5 files that have the same data, but newer versions have different Table Field names. Imagine 'col_1' in version 1 is called 'Column_1' in version 2. Same data, different name. (I receive this data downstream from another process, so can't control the names.) I want to do a table.read_where() with a condition that references this column.

I found one way to do it (below):
if version == 1:
    arr
= dtable.read_where('col_1 == id')
else:  
    arr
= dtable.read_where('Column_1 == id')

In anticipation of future changes, I'd like to use a variable to do the same thing.
Something like this:
if version == 1:
    read_col
= 'col_1'
else:  
    read_col
= 'Column_1'
# Note: This line does not work as-is:
arr
= dtable.read_where( read_col == id )

Is there a way to write this table.read_where() condition with a variable?
If so, I'm missing something.
Thanks.
--Ken

Kyle Robertson

unread,
Sep 28, 2019, 10:01:49 PM9/28/19
to pytables-users
You could just use string formatting to create the conditional ad a string using the value of the variable. So


if version == 1:
read_col = 'col_1'
else:
read_col = 'Column_1'
# Note: This line does not work as-is:
condition = “{} == id”.format(read_col)
arr = dtable.read_where( condition)

Ken Walker

unread,
Sep 29, 2019, 3:39:03 PM9/29/19
to pytables-users
Hi Kyle,
Thanks for the suggestion. I'm laughing @ me for the simplicity of the solution.
I tinkered with all kinds of Pytables methods to do this, and missed the obvious Python solution!
Again, thanks.
-Ken
Reply all
Reply to author
Forward
0 new messages