Python Forum
Python beginner that needs an expression added to existing script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python beginner that needs an expression added to existing script
#1
I have a python script that scrubs emails and works well.

I need to add the ability to remove everything including and after the string "Still have questions" after the second def of "removte_http_links" in the following part of the script.

def extract_http_links(payload):
    # pattern = r'https?://[^\s<>"]+|www\.[^\s<>"]+'
    pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+=]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
    http_links = re.findall(pattern, payload)
    return http_links

def remove_http_links(payload):
    #pattern = r'https?://[^\s<>"]+|www\.[^\s<>"]+'
    pattern = r'<http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+=<>]|[\r\n]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
    payload_without_links = re.sub(pattern, '', payload)
    pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+=<>]|[\r\n]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
    payload_without_links = re.sub(pattern, '', payload_without_links)
    return payload_without_links
NEW EXPRESSION TO BE PUT IN HERE IN THE PROCESS

def send_all_email(message_all , subject):
    print(subject)
    print(message_all.encode('utf-8'))
    msg = MIMEMultipart()
    msg['From'] = username
    msg['To'] = forward_address
#   msg['Subject'] = subject
    msg.attach(MIMEText(message_all))
    mail = smtplib.SMTP(smtp_server, smtp_port)
    mail.ehlo()
    mail.starttls()
    mail.ehlo()
    mail.login(username, password)
    mail.sendmail(username, forward_address, msg.as_string())
    mail.quit()
I would appreciate any input to accomplish this task in the sequence above.

Thanks in advance.

Mark
buran write Sep-04-2023, 03:29 AM:
Restored last meaningful content.
Watch your language.
Yoriz write Aug-27-2023, 05:07 PM:
Please post all code, output and errors (in its entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
I presume your email is a text file or string. A simple way to achieve this is .split()

myemail = """
All kinds of email info here

https://python-forum.io/thread-40619.html

I was visiting friends in Princeton one Saturday in the 1950s when our
host asked his son-in-law, Bill Bennett, and me (Bruce) if we’d like to
spend the evening with his friend, Albert Einstein. Two awed physics grad-
uate students soon waited in Einstein’s living room as he came downstairs
in slippers and sweatshirt. I remember tea and cookies but not how the
conversation started.

Still have Questions

Einstein soon asked about our quantum mechanics course. He
approved of our professor’s choice of David Bohm’s book as the text, and
he asked how we liked Bohm’s treatment of the strangeness quantum
theory implied.
"""

result = myemail.split('Still have Questions')[0]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python best library for Excel reports & review of existing code MasterOfDestr 4 679 Feb-14-2024, 03:39 PM
Last Post: MasterOfDestr
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,280 Jun-29-2023, 11:57 AM
Last Post: gologica
  [SOLVED] [BS] Why new tag only added at the end when defined outside the loop? Winfried 1 976 Sep-05-2022, 09:36 AM
Last Post: snippsat
  Python Regular expression, small sample works but not on file Acernz 5 2,955 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Using Regex Expression With Isin in Python eddywinch82 0 2,299 Apr-04-2021, 06:25 PM
Last Post: eddywinch82
  How to add an image to an existing facebook post using python graph API? Ascalon 0 2,239 Dec-01-2020, 08:33 PM
Last Post: Ascalon
  httplib2 - how to see credentials added by add_credentials? MSV 2 2,173 Aug-05-2020, 12:24 PM
Last Post: MSV
  Python Paramiko mkdir command overwriting existing folder. How can i stop that? therenaydin 1 3,235 Aug-02-2020, 11:13 PM
Last Post: therenaydin
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,939 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,314 May-28-2020, 05:27 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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