Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:59:52+00:00 2026-06-14T16:59:52+00:00

There is more to the code but I have it sort and read a

  • 0

There is more to the code but I have it sort and read a specific email (but this email message changes and more things are added to the email message in an ordered format that looks like a list but is not a physical list that is able to be labeled..)

for num in data[0].split():
        typ, msg_data = conn.fetch(num, '(RFC822)')
        for response_part in msg_data:
            if isinstance(response_part, tuple):
                msg = email.message_from_string(response_part[1])
                subject=msg['subject']                   
                payload=msg.get_payload()
                body=extract_body(payload)
                print(body)
    #the portion of the code is these sources:unutbu and Doug Hellmann's tutorial on  imaplib

when it prints it prints:

Run script8.py


Run task9.py


Play asdf.mp3


Run unpause.py

but it changes so if I ran it ten minutes from now it may say:

Run script8.py


Run task9.py


Play asdf.mp3


Run unpause.py


Run newscript88.py

And I need it to take what is printed from the above code and pull the last 2 words which in this example would be Run newscript88.py and label it as a string to later be put into code like this:

os.startfile('Run newscript88.py')

So literally it would look take the last 2 words from the email message then it would put those last 2 words into this:

    os.startfile('last 2 words')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:59:54+00:00Added an answer on June 14, 2026 at 4:59 pm

    You want the last two words from the body, which you have as a string in the variable body, right?

    The exact answer depends on how you define “word”, but here’s a very simple answer:

    lastTwoWords = body.split()[-2:]
    

    If you print that, you’ll get something like ['Run', 'newscript88.py']. To put that back into a string, just use join:

    os.startfile(' '.join(lastTwoWords))
    

    From your sample data, it seems at least possible that the last “word” could contain spaces, and what you really want is the two words on the last line… so maybe you want something like this:

    lastLine = body.split('\n')[-1]
    lastTwoWords = lastLine.split(None, 1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anything wrong with this code or can this be done more efficiently?
Is there more elegant (less code) way of find a matrix OUT, with colSums(OUT)<=a
There Currently is a local debate as to which code is more readability We
in the code below at first if statements block (there will be more than
I was wondering if there was a more efficient (efficient as in simpler/cleaner code)
I'm trying to reorganize my code and make it more unobstrusive. Mainly, there is
First off, I have read through a list of postings on this topic and
I'm programmer-beginner, but I want to understand the things a bit more deeply. I
All- I have never done this sort of thing before and am very confused.
I have a class that generates random IP addresses. I have to sort this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.