Formatting triple quote?

2050
10
Jump to solution
08-30-2018 01:30 PM
by Anonymous User
Not applicable

I'm creating a Python script to create a HTML document with the message containing HTML code in triple quotes. How would I go about formatting some of the text inside the triple quote? 

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

I figured it out with a simple solution.

I exit the triple quotes with triple quotes then added my variables by using + "{}".format(row[0]) + then re adding triple quotes to continue the string.

View solution in original post

0 Kudos
10 Replies
DanPatterson_Retired
MVP Emeritus

mess with single and double quotes and escaping... at least in python 3

a = """ \'hello\' said "the \"parrot\" """

print(a)
 'hello' said "the "parrot" 

a
' \'hello\' said "the "parrot" '
0 Kudos
DanPatterson_Retired
MVP Emeritus

but if you want to keep the triple quotes then you have to single quote the triple quotes so that they appear but not affect the escaped quotes…

a = '""" \'hello\' said "the \"parrot\" """'

print(a)
""" 'hello' said "the "parrot" """

a
Out[8]: '""" \'hello\' said "the "parrot" """'

simple ehh???  

by Anonymous User
Not applicable

In the Python script below I'm creating an HTML document with a hyperlink to generate an email. How would I use formatting .format(row[0]) in the body section of the email?

import arcpy, datetime, os
arcpy.env.overwriteOutput = True

points = r"***********" 
today = datetime.date.today() 
dte = today.strftime('%m/%d/%Y') 
dtep = today.strftime('%m%d%Y') 
fields = ['Applicant', 'Date_Final', 'Email', 'Permit_No'] 

arcpy.MakeFeatureLayer_management(points, 'FinalizedApproaches') 

f= open("Finalized_{}.html".format(dtep),"w+")

with arcpy.da.SearchCursor(points, fields, "Date_Final = CURRENT_DATE") as cursor: 
    for row in cursor:
        message = """<!DOCTYPE html>
<html>
<head>
<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
</head>
<body>

<h2>Notification Report</h2>

<table>
  <tr>
    <th>Customer</th>
    <th>Status</th>
    <th>Date</th>
    <th>Email</th>
  </tr>
  <tr>
    <td>Test Name</td>
    <td>Finalized</td>
    <td>08/30/2018</td>
    <td><a href="mailto:test@email.com&subject=Permit Status: Finalized&body={}",%0D%0AThis is to notify you that your approach has been finalized. Please use the link below to view your permit documentation.%0D%0A%0D%0Ahttps://www.arcgis.com">Notify</a></>
  </tr>
</table>

</body>
</html>"""



f.write(message) 

f.close()

print "{} Finalized report generate".format(dte)
0 Kudos
DanPatterson_Retired
MVP Emeritus

did you try some of the suggestions for formatting within the triple quote?

What did your code produce

0 Kudos
by Anonymous User
Not applicable

I tested your suggestions and it still just produces {} in the body of the email.

0 Kudos
DanPatterson_Retired
MVP Emeritus

perhaps you will have to google, …. html email python

I don't think it is just the triple quotes

For example

Sending HTML email using Python - Stack Overflow 

0 Kudos
by Anonymous User
Not applicable

I'm really just looking to have an email composed based on a fields from a search cursor using python but I want to be able to send it manually.

0 Kudos
DanPatterson_Retired
MVP Emeritus

a and b would be inputs from your cursor???

Sorry, but maybe Joe Borgione‌ might have had to do email notification or knows someone that does

 a = "Hey folks..."
b = "\nhttp://www.python.org"
t = "{}\nHow are you?\nHere is the link you wanted:{}".format(a, b)
h = """\
<html>
  <head></head>
  <body>
    <p>{}
       How are you?<br>
       Here is the link you wanted.
       {}
    </p>
  </body>
</html>
""".format(a, b)



print("{}\n{}".format(t, h))
Hey folks...
How are you?
Here is the link you wanted:
http://www.python.org

<html>
  <head></head>
  <body>
    <p>Hey folks...
       How are you?<br>
       Here is the link you wanted.
       
http://www.python.org
    </p>
  </body>
</html>
by Anonymous User
Not applicable

I figured it out with a simple solution.

I exit the triple quotes with triple quotes then added my variables by using + "{}".format(row[0]) + then re adding triple quotes to continue the string.

0 Kudos