Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

How to escpate double quotes inside of single quotes in Python

Posted on 2014-03-07
6
Medium Priority
?
933 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1500 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 20

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Fine Tune your automatic Updates for Ubuntu / Debian
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Progress
Suggested Courses

647 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