Python Forum
smtplib: string formatting not carrying over to email
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
smtplib: string formatting not carrying over to email
#1
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:
   

Which as you can see is a bit off in its formatting
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  smtplib send email has no timestamp Pedroski55 2 2,006 Jun-11-2024, 04:57 AM
Last Post: Pedroski55
  Formatting DateTime string and and converting it from AM/PM to 24 hours tester_V 2 1,284 Jun-08-2024, 05:16 PM
Last Post: tester_V
  Formatting a date time string read from a csv file DosAtPython 5 5,538 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  String formatting (strptime) issues Henrio 2 1,981 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  confused about string formatting barryjo 7 3,591 Mar-06-2022, 02:03 AM
Last Post: snippsat
  string formatting barryjo 7 3,799 Jan-02-2022, 02:08 AM
Last Post: snippsat
  Help with string formatting in classes brthurr 6 14,365 Dec-17-2021, 04:35 PM
Last Post: Jeff900
  Sending Attachments via smtplib [SOLVED] AlphaInc 3 11,444 Oct-28-2021, 12:39 PM
Last Post: AlphaInc
  Sending random images via smtplib [SOLVED] AlphaInc 0 2,476 Oct-19-2021, 10:10 AM
Last Post: AlphaInc
  [UnicodeEncodeError from smtplib] yoohooos 0 4,621 Sep-25-2021, 04:27 AM
Last Post: yoohooos

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020