Python Forum
Removing the unwanted data from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing the unwanted data from a file
#11
Just a small change to read the file into an array; works fine.

import re
 
# read file into an array 
f = open('https.txt', 'r+')
data = f.read()
f.close()

pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
for match in pattern.finditer(data):
    print(match.group())
    
Thanks everyone for your help. Smile
Reply
#12
(Nov-14-2021, 09:11 PM)jehoshua Wrote: Just a small change to read the file into an array; works fine.
Just a small correction you are not reading the file into a array(we call it list in Python),
when do f.read() read all into a string,which is okay it will work fine.

Let say don't want not want read all contend into memory,but do it line bye line and not close file bye using with open.
Then it will be like this.
import re

pattern = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
with open('https.txt', 'r+') as f:
    for line in f:
        for match in pattern.finditer(line):
            print(match.group())
jehoshua likes this post
Reply
#13
Thanks, that produced exactly the same output. Smile
Reply
#14
I think this article can help u just check once: https://www.scaler.com/topics/find-function-in-python/
jehoshua likes this post
Reply
#15
@codinglearner - thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Unwanted execution of unittest ThomasFab 9 2,060 Nov-15-2022, 05:33 PM
Last Post: snippsat
  HELP on Unwanted CSV Export Output | Using Selenium to Scrape soothsayerpg 0 1,274 Jun-13-2021, 12:23 PM
Last Post: soothsayerpg
  xml file creation from an XML file template and data from an excel file naji_python 1 2,119 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 2,113 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  How to eliminate unwanted spaces Mohan 5 2,891 Jun-04-2020, 08:34 AM
Last Post: buran
  Removing Certain Numbers From File chascp 2 2,100 Feb-07-2020, 04:04 PM
Last Post: chascp
  Unwanted delay between looped synth plays WolfeCreek 1 2,318 Aug-02-2018, 09:24 PM
Last Post: Vysero
  Unwanted variable change in module dannyH 2 2,685 May-08-2018, 05:33 PM
Last Post: dannyH
  Unwanted random generation of scripted Shapes in GASP diemildefreude 3 5,180 Oct-23-2016, 03:11 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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