-1

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!

cs95
  • 379,657
  • 97
  • 704
  • 746
Emm Gee
  • 165
  • 7
  • Your string doesn't actually have those double backslashes in it, so the problem you're trying to solve doesn't exist. You're just being confused because the _repr_ of the string—the thing that Python shows you when you evaluate the string at the interactive prompt—adds quotes around it and backslash escapes within it. But those aren't part of the string, they're just added by `repr`. – abarnert Aug 16 '18 at 19:21
  • When you do `print(s)`, the `print` function just calls `str` on each argument, so if you want to get the exact same string that `print` is printing, you can use `str(s)`. But if `s` is already a string, there's no point in doing this—`str(s)` just returns `s` in that case, so you can just use `s` as-is. – abarnert Aug 16 '18 at 19:23
  • The actual problem here is that your `queryStr` is _already_ the repr of an actual string. You don't want even _single_ backslashes, and you don't want quotes around it. Somewhere, you did something wrong in creating that string, and _that_ is what you need to fix, not the code that uses that string later. – abarnert Aug 16 '18 at 19:25
  • Also @abarnert df.query(str()) is not supported. – Emm Gee Aug 16 '18 at 19:36
  • What do you mean by that? `df.query(str())` is just querying an empty string. But `df.query(str(something))` is fine. It's not often _useful_, because `something` is usually already a string (as it is in your case), but it's certainly _supported_. – abarnert Aug 16 '18 at 20:39
  • Meanwhile, your edit is wrong. There are _not_ backslashes in front of the quotes around the repr of a string (unless the string has both single and double quotes in it). See [this example on repl.it](https://repl.it/repls/SunnyMarriedNamebinding). – abarnert Aug 16 '18 at 20:42
  • Also, "I quite literally need to somehow 'grab' the output of print" quite literally is just using `str`—which is pointless, because it will give you exactly the same thing you already have. – abarnert Aug 16 '18 at 20:42

1 Answers1

-1

If you prefix the string with 'r', it will indicate to Python that it's a "raw string" and will pass the string exactly as you specify it:

df.query(r"your \query")

This will pass the string 'y', 'o', 'u', 'r', ' ', '\', 'q', 'u', 'e', 'r', 'y' -- with the one backslash exactly as you specfiy it.

From the Python docs: https://docs.python.org/3/reference/lexical_analysis.html

Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters.

payne
  • 13,833
  • 5
  • 42
  • 49
  • Thank you for your input. Unfortunately, there are two problems: The first is that I'm using python 2.**, not python 3. The second is that `>>> df.query(r'{}'.format(queryStr))` treats it as `(1522000002801<=TIMESTAMP<=1522000034174 or 1522000048441<=TIMESTAMP<=1522000074589) and (RESOURCE=='GTEX' or RESOURCE=='MOD_FLYBASE') and (ACTIVITY=='REQ_RESOURCE')` (NO Backslashes) and throws an error. Thoughts? – Emm Gee Aug 16 '18 at 19:21
  • Raw strings are in Python 2: https://docs.python.org/2/reference/lexical_analysis.html – payne Aug 16 '18 at 22:20