Python Forum
how to just filter .mp4 in the text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to just filter .mp4 in the text file
#1
i have a test1111.txt, which have some html tag, and i wish to just filter .mp4, other word i don't want.
How to let it just print https://ttt111/tr/event/1234/1912/test1122.mp4



test1111.txt:
</div>
<div class="video-name " data-id="3" data-video="https://ttt111/tr/event/1234/1912/test1122.mp4" data-cover="https://ttt111/tr/event/123/19121/4_3_.png">
<div class="video-name__title">Chapter 4</div>
<div class="video-name__description">test</div>
</div>
<div class="video-name " data-id="4" data-video="https://ttt111/tr/event/1223/1912.mp4" data-cover="https://ttt111/tr/event/123/event/1122/1912/5_.png">
<div class="video-name__title">Chapter 5</div>
<div class="video-name__description">test</div>


searchfile = open("test111.txt", "r", encoding='utf-8')
for line in searchfile:
    if '.mp4' in line: 
        print (line)
searchfile.close()
Reply
#2
why not use BeautifulSoup?
from bs4 import BeautifulSoup
file_name = 'test1111.txt'
with open(file_name) as f:
    soup = BeautifulSoup(f.read(),'html.parser')
    for div in soup.find_all('div', {'class':'video-name'}):
        print(div.get('data-video'))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,111 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  filter every 24 days file (considering file name) RolanRoll 5 2,211 Jun-23-2022, 11:41 AM
Last Post: RolanRoll
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,654 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Converted Pipe Delimited text file to CSV file atomxkai 4 6,955 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Filter Excel and Convert an Excel File giddyhead 0 2,216 May-13-2021, 06:31 PM
Last Post: giddyhead
  [split] How to convert the CSV text file into a txt file Pinto94 5 3,326 Dec-23-2020, 08:04 AM
Last Post: ndc85430
  Saving text file with a click: valueerror i/o operation on closed file vizier87 5 4,394 Nov-16-2020, 07:56 AM
Last Post: Gribouillis
  saving data from text file to CSV file in python having delimiter as space K11 1 2,392 Sep-11-2020, 06:28 AM
Last Post: bowlofred
  Web Form to Python Script to Text File to zip file to web wfsteadman 1 2,133 Aug-09-2020, 02:12 PM
Last Post: snippsat
  Convert Excel file to Text file marvel_plato 6 19,658 Jul-17-2020, 01:45 PM
Last Post: marvel_plato

Forum Jump:

User Panel Messages

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