EDIT: SOLUTION!!!
As it turns out, all I needed to do for this to work was df.query('{}'.format(eval(queryStr)))
and Python treated queryStr
as if it were the same as print(queryStr)
. I don't recommend using eval
all of the time, but in this case it was the best solution for me that I could figure out so far.
ORIGINAL PROBLEM:
I have a string (queryStr
):
>>> queryStr
"'(1522000002801<=TIMESTAMP<=1522000034174 or
1522000048441<=TIMESTAMP<=1522000074589) and (RESOURCE==\\'GTEX\\' or
RESOURCE==\\'MOD_FLYBASE\\') and (ACTIVITY==\\'REQ_RESOURCE\\')'"
(Note the backslashes--there are 2)
When I print it queryStr
, I get:
>>> print(queryStr)
'(1522000002801<=TIMESTAMP<=1522000034174 or
1522000048441<=TIMESTAMP<=1522000074589) and (RESOURCE==\'GTEX\' or
RESOURCE==\'MOD_FLYBASE\') and (ACTIVITY==\'REQ_RESOURCE\')'
Which 1 backslash-- EXACTLY what I want to pass to df.query()
.
Because when I run df.query(queryStr)
(running df.query()
with two backslashes), I get an error. But when I run:
df.query('(1522000002801<=TIMESTAMP<=1522000034174 or 1522000048441<=TIMESTAMP<=1522000074589) and (RESOURCE==\'GTEX\' or RESOURCE==\'MOD_FLYBASE\') and (ACTIVITY==\'REQ_RESOURCE\')')
(df.query()
with 1 backslash)
I get exactly what I want:
TIMESTAMP NODE ID REFID USER ACTIVITY RESOURCE
1 1522000016966 1 3 3 6 REQ_RESOURCE MOD_FLYBASE
4 1522000024848 1 6 6 10 REQ_RESOURCE GTEX
So, my question is:
How can I capture the 'output' of print(queryStr)
and pass it to df.query()
, exactly as it is printed?
I know that I can pass the output of print
to a file, and then bring in those lines, but I feel like that's extremely inefficient.
Thank you!!
EDIT:
Please note that queryStr
is NOT the .__repr__()
of the string--because then there would be backslashes in front of the first and last quotes. Hence, using solutions involving .__repr__()
or .__str__()
have not worked. I quite literally need to somehow 'grab' the output of print
.
Thank you again!