Jacek Perry
Jacek Perry

Reputation: 413

Python, quotes within a quote

I am trying to build a nagios script but having an issues with a single quote inside a double one.

I am trying to pass the following:

select (select count(*) from v$session)/(select value from v$parameter where name = 'processes')*100 Percent from dual"

The select has to be quoted just like above.

I tried to pass this into the string in various ways:

line=select (select count(*) from v$session)/(select value from v$parameter where name = 'processes')*100 Percent from dual"

than created a command like this:

process= '"' + line + '"'

p = subprocess.Popen( line.split())

Unfortunately line.split doesn't do correct quote parsing.

The "select" must be quoted, and 'processes' must be quoted in a single quote.

Any suggestion how I could do this?

Update:

Apparently the issue is with the "split" process. The sql statement has to be passed intact, and quoted as a single statement. However I have pass quite a few complicated arguments and the line.split worked for that, until I run into this issue.

So the better question is how I can format the subprocess to send a whole string as well as a long list (about 5 string arguments) with it?

Update:

In case this helps someone. Apparently with Popen you have to divide arguments and input. So that for instance you can't send "--test blah"; it has to be "--test", "blah". This means that to send a string you have to split it.

However if a whole string is necessary you have to edit that and append to the list.

So for instance

a = "--test blah --something other"
b = a.split()

This will generate a list and Popen will handle it fine (with subprocess.Popen(b)).

However if you need to also send a whole string like this:

sql = "select count(*) from v$session"

you can't just add this to the "a string" as in a + sql. You need to do something like this:

b.append(sql)

which will add the whole string and then Popen knows how to handle it.

Unfortunately (at least to the best of my knowledge) it is not possible to send Popen a string such as:

c = a + sql
subprocess.Popen(c)

and get a response unless you use shell=True. However, then you get return statement and NOT output.

Hope this will help someone in similar situation as I was.

Upvotes: 3

Views: 3963

Answers (2)

Makoto
Makoto

Reputation: 106528

If I understand what you're asking, then you want to be able to have quotes around select, and also escape the single quotes around processes.

Try using triple quotes:

line=""" "select (select count(*) from v$session)/(select value from v$parameter where name = \'processes\')*100 Percent from dual" """

...which prints:

"select (select count(*) from v$session)/(select value from v$parameter where name = 'processes')*100 Percent from dual"

Upvotes: 4

yedpodtrzitko
yedpodtrzitko

Reputation: 9359

  • use triple quotes:

"""valid single ' and double quotes " in string"""

or

'''valid single ' and double quotes " in string'''

  • if you need use quotes in single quotes, escape them with backslash:

"valid \" string"

Upvotes: 2

Related Questions