Python Forum

Full Version: smtplib: string formatting not carrying over to email
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The string formatting I specify in python doesn't carry over perfectly to email when I send it via smtplib.

def main():
    pull()   # pulls html grades from university website
    graded_courses = scrape()  # scrapes html 
    row_fmt = '{0:10}{1:4}{2:6}{3}\n'
    body = ""
    for course in graded_courses:
        body += row_fmt.format(course.name, 'f'+course.percentage_grade, course.letter_grade, 'g'+course.avg) #grades are not released yet, so 'f' and 'g' are just for demo purposes (course.percentage_grade, course.avg are empty strings)
    print(body)
    send_email(body)
Output:
CSC148H1 f IPR g CSC165H1 f IPR g MAT137Y1 f IPR g MAT224H1 f IPR g SMC212H1 f IPR g Email sent
However my gmail display shows this:
[attachment=844]

Which as you can see is a bit off in its formatting
Generally for a terminal, you make things line up with spaces, and assume a monospaced font in use.

For email, I would assume HTML parsing and proportionally spaced fonts. You could try wrapping your output in <pre></pre> tags and see if the email renders it in a monospace font. Or, create a real HTML table and spit out the tags for that (and there are several modules that would assist with printing HTML tables)