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')
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:
If you print that, you’ll get something like
['Run', 'newscript88.py']. To put that back into a string, just usejoin: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: