Solved

How to escpate double quotes inside of single quotes in Python

Posted on 2014-03-07
6
743 Views
Last Modified: 2014-04-30
Hi guys,

I was a python script that finds certain text and replaces it, however I can't seem to figure out how to escape double quotes inside of single quotes. Any ideas?

python - <<END
import string
s = open("/etc/init/control-alt-delete.conf").read()
s = s.replace('exec /sbin/shutdown -r now "Control-Alt-Delete pressed"', 'exec /usr/bin/logger -p security.info "Ctrl-Alt-Delete pressed"')
f = open("/etc/init/control-alt-delete.conf", 'w')
f.write(s)
f.close()
END

Open in new window

0
Comment
Question by:Tbalz
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39913168
Why do you need to escape them? You only need to escape quotes that match the quotes you use to wrap the whole string. If your outer quotes are single quotes, then any internal single quotes need to be escaped. Likewise with double quotes.

e.g.

 Screenshot
0
 
LVL 29

Expert Comment

by:pepr
ID: 39913192
To add... Even when you need to write both single and double quotes inside a string literal, you do not need to escape them. You can enclose the string value to the triple quotes -- using tripled or single quotes or double quotes like this:

s = """He said: "That's not a problem""""
0
 

Author Comment

by:Tbalz
ID: 39913550
Yes but it still doesn't work for me. This example has double quotes inside of single quotes.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39913635
Perhaps if you tell or show us the error?
0
 
LVL 5

Accepted Solution

by:
Pasha Kravtsov earned 500 total points
ID: 40006915
Try this
python - <<END
import string
s = open("/etc/init/control-alt-delete.conf").read()
s = s.replace('exec /sbin/shutdown -r now \"Control-Alt-Delete pressed\" ', 'exec /usr/bin/logger -p security.info \"Ctrl-Alt-Delete pressed\" ')
f = open('/etc/init/control-alt-delete.conf', 'w')
f.write(s)
f.close()
END

Open in new window

0
 
LVL 19

Expert Comment

by:simon3270
ID: 40014376
It's probably not the escape characters (you *can* have them before double quotes, but you don't need to and they are simply swallowed by Python).

The problem is possibly because you have a space after the second double quote - if you input file doesn't have that space, the original pattern won't match, so it won't be replaced.

So, just change that text to
    pressed\"',

(and remove the backslashes if you like)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question