Solved

How to escpate double quotes inside of single quotes in Python

Posted on 2014-03-07
6
796 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Starting up a Project

726 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