How to escpate double quotes inside of single quotes in Python

Posted on 2014-03-07
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 "Ctrl-Alt-Delete pressed"')
f = open("/etc/init/control-alt-delete.conf", 'w')

Open in new window

Question by:Tbalz
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
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.


LVL 29

Expert Comment

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""""

Author Comment

ID: 39913550
Yes but it still doesn't work for me. This example has double quotes inside of single quotes.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 75

Expert Comment

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

Accepted Solution

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 \"Ctrl-Alt-Delete pressed\" ')
f = open('/etc/init/control-alt-delete.conf', 'w')

Open in new window

LVL 19

Expert Comment

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

(and remove the backslashes if you like)

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse with various Java releases 7 86
Java pass by reference 3 74
Best laptop programming computer for Windows development 14 86
How ldap located a Domain controller? 22 77
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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