Solved

How to escpate double quotes inside of single quotes in Python

Posted on 2014-03-07
6
683 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 74

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 28

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 74

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now