[Tutor] loops, variables and strings

Monika Jisswel monjissvel at googlemail.com
Sat Jul 5 20:11:04 CEST 2008


maybe StringIO ?

MsgBody = StringIO.StringIO()
print >> MsgBody, "Hello. Below is an automated e-mail with important
statistics."
print >> MsgBody, ""        #empty line
print >> MsgBody, ""        #empty line
print >> MsgBody, "The following user accounts expired today:"
print >> MsgBody, "<user1> - <Full Name>" % (var1, Name1)
print >> MsgBody, "<user2> - <Full Name 2>" % (var2, Name2)
...
< More code here >
...

MSGBODY = MsgBody.getvalue()  # use MSGBODY in you Email.




2008/7/5 James <jtp at nc.rr.com>:

> All,
>
> I'm trying to do something pretty simple, but I can't seem to get
> Python to behave nicely. :)
>
> I'd like to automate a script that sends out three or four lists in an
> e-mail. I have a function dedicated to sending e-mail that will take a
> string variable, slap it in a message, and send it on its merry way.
>
> The issue, however, is that I can't format the string I want
> correctly. I'd like to do something similar to the following:
>
> myString = """Hello. Below is an automated e-mail with important
> statistics.
>
> The following user accounts expired today:
> <user1> - <Full Name>
> <user2> - <Full Name 2>
> ...
>
> The following user accounts will expire within the next 7 days:
> <user3> - <Full Name 3>
> <user4> - <Full Name 4>
> ...
>
> The following user accounts will expire in the next month:
> <user 5> - <Full Name 5>
> <user 6> - <Full Name 6>"""
>
> I've written some code that actually does include a list of users, like
> this:
>
> myString = """Hello. Below is an automated e-mail with important
> statistics.
>
> The following user accounts expired today:
> %s
>
> The following user accounts will expire within the next 7 days:
> %s
>
> The following user accounts will expire in the next month:
> %s""" % (expiredAccounts,toExpire7,toExpire30,),
>
> Of course in this scenario, the variables in the parenthesis are
> strings with newlines in them. But when I do a 'print myString', it
> shows everything in one line, including the '\n' characters at the end
> of every user account listed.
>
> Thoughts on what I may be doing wrong?
>
> Thanks!
> - j
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080705/3a81ec6d/attachment.htm>


More information about the Tutor mailing list