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
  Formatting a date time string read from a csv file DosAtPython 5 1,285 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  String formatting (strptime) issues Henrio 2 842 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  confused about string formatting barryjo 7 1,989 Mar-06-2022, 02:03 AM
Last Post: snippsat
  string formatting barryjo 7 2,062 Jan-02-2022, 02:08 AM
Last Post: snippsat
  Help with string formatting in classes brthurr 6 9,255 Dec-17-2021, 04:35 PM
Last Post: Jeff900
  Sending Attachments via smtplib [SOLVED] AlphaInc 3 2,152 Oct-28-2021, 12:39 PM
Last Post: AlphaInc
  Sending random images via smtplib [SOLVED] AlphaInc 0 1,688 Oct-19-2021, 10:10 AM
Last Post: AlphaInc
  [UnicodeEncodeError from smtplib] yoohooos 0 3,389 Sep-25-2021, 04:27 AM
Last Post: yoohooos
  Question on HTML formatting with set string in message Cknutson575 3 3,492 Mar-09-2021, 08:11 AM
Last Post: Cknutson575
  Understanding The Arguments for SMTPlib - sendmail JoeDainton123 3 2,719 Aug-03-2020, 08:34 AM
Last Post: buran

Forum Jump:

User Panel Messages

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