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
  Convert any Python expression to a string voidtrance 2 1,078 Jun-23-2025, 07:06 AM
Last Post: DeaD_EyE
  Unequal values when added with 0? Azdaghost 1 748 May-24-2025, 12:24 AM
Last Post: deanhystad
  Create a new subclass in a Python extension based on an existing class voidtrance 6 2,610 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  Python best library for Excel reports & review of existing code MasterOfDestr 4 11,974 Feb-14-2024, 03:39 PM
Last Post: MasterOfDestr
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 10,065 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 1,924 Sep-05-2022, 09:36 AM
Last Post: snippsat
  Python Regular expression, small sample works but not on file Acernz 5 5,269 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Using Regex Expression With Isin in Python eddywinch82 0 3,481 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 3,669 Dec-01-2020, 08:33 PM
Last Post: Ascalon
  httplib2 - how to see credentials added by add_credentials? MSV 2 3,433 Aug-05-2020, 12:24 PM
Last Post: MSV

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.