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
  data input while debugging causes unwanted code completion fred1232 2 2,591 Sep-14-2025, 03:32 PM
Last Post: deanhystad
  How to remove unwanted images and tables from a Word file using Python? rownong 2 2,155 Feb-04-2025, 08:30 AM
Last Post: Pedroski55
Question Unwanted execution of unittest ThomasFab 9 6,192 Nov-15-2022, 05:33 PM
Last Post: snippsat
  HELP on Unwanted CSV Export Output | Using Selenium to Scrape soothsayerpg 0 2,184 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 3,400 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 3,007 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  How to eliminate unwanted spaces Mohan 5 5,278 Jun-04-2020, 08:34 AM
Last Post: buran
  Removing Certain Numbers From File chascp 2 3,683 Feb-07-2020, 04:04 PM
Last Post: chascp
  Unwanted delay between looped synth plays WolfeCreek 1 3,344 Aug-02-2018, 09:24 PM
Last Post: Vysero
  Unwanted variable change in module dannyH 2 4,067 May-08-2018, 05:33 PM
Last Post: dannyH

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.