0

I have a piece of standard text having multiple lines and some format like below:

owner: oracle
date_conditions: 1
    timezone: US/Eastern
    std_out_file: "/app/local/job.log"
    machine: rachost

I want to use this above text with machine and timezone being changed as per the need. How can I use python to define this is a template and substitute various values? I came across the substitution option in string.Template. But this seems to be working with a single line only.

martineau
  • 119,623
  • 25
  • 170
  • 301
vivek
  • 111
  • 7
  • This template should be read to be used else where, or this is the one you want to produce? I know you said read it, but you mention that you want to write it as well. – Florian Bernard Oct 10 '19 at 15:03
  • Which values should be replaced by which values? Why not use `%(key)s` (or the corresponding with `format`), thus having a better template to plug in a dictionary?! – dan_fulea Oct 10 '19 at 15:03
  • @FlorianBernard i wanted to use this template and generate new text having similar format with new values for machine or timezone.I need to use this new text file to send it via mail. – vivek Oct 10 '19 at 15:11

1 Answers1

0

The string.Template class can handle strings consisting of multiple lines.

For example:

from string import Template

template = Template('''\
owner: oracle
date_conditions: 1
    timezone: $tz
    std_out_file: "/app/local/job.log"
    machine: $mach
''')

result = template.substitute(tz='US/Pacific', mach='foobar')
print(result, end='')

Prints:

owner: oracle
date_conditions: 1
    timezone: US/Pacific
    std_out_file: "/app/local/job.log"
    machine: foobar
martineau
  • 119,623
  • 25
  • 170
  • 301
  • This works fine and is exactly what i needed. What is the purpose of end=' ' in the print statement? – vivek Oct 11 '19 at 08:05
  • The `end=''`suppresses the newline `print()` adds by default. It's not needed because the template string ends with one. If the lines of the template were read from a file, the last line would likely also have one, so you might want to use this in that scenario, as well. – martineau Oct 11 '19 at 09:16
  • how can we send the 'result' to an email. – vivek Oct 15 '19 at 10:01
  • @vivek: Sorry, but that's a completely unrelated question and it doesn't sound like you've made any attempt to do it yourself. Suggest you take a look at the documentation of Python's [`smtplib`](https://docs.python.org/3/library/smtplib.html#module-smtplib) module. – martineau Oct 15 '19 at 11:35
  • i have a function for sending the email: def sendjilmail(email_address='vievk@abc.com'): global glob_email_msg msg = MIMEMultipart() msg['From']= 'check@abc.com' msg['To'] = email_address msg['Subject'] = 'missing file Email' msg.attach(MIMEText(glob_email_msg,'html')) s = smtplib.SMTP('mailhost.jpmchase.net') s.starttls() s.sendmail('check@abc.com', email_address, msg.as_string()) s.quit() But the issue is that it is not retaining the exact format of the file with spaces as required. – vivek Oct 15 '19 at 14:11