Python stripping words upon specific condition in a list of sentences

Viewed 57

My starting file was .txt one, that looked like:

https://www.website.com/something1/id=39494 notes !!!! other notes
https://www.website2.com/something1/id=596774 ... notes2 !! other notes2

and so on.. so very messy

to clean it up I did:

import re

with open('file.txt', 'r') as filehandle:
    places = [current_place.rstrip() for current_place in filehandle.readlines()]

filtered = [x for x in places if x.strip()]

This gave me a list of websites (without spaces in between) but still with notes in the same string.

My goal is the first have a list of "cleaned" websites without any notes afterwords:

https://www.website.com/something1/id=39494 
https://www.website2.com/something1/id=596774

For that I thought to target the space after the end of website and get rid of all the words afterwords:

for s in filtered:
    f = re.search('\s')

This returns an error, but even if it worked it wouldn't return what I thought.

The second step is to strip the website of some characters and compose it like: https://www.website.com/embed/id=39494

but this would come later.

I just wonder how can I achieve the first step and get rid of the notes after the website and have a clean list.

3 Answers

If each line consists of a URL followed by a space and any other text, you can simply split by the space and take the first element of each line:

urls = []
with open('file.txt') as filehandle:
  for line in filehandle:
    if not line.strip(): continue # skip empty lines
    urls.append(line.split(" ")[0])

# now the variable `urls` should contain all the URLs you are looking for

EDIT: second step

for url in urls:
  print('<iframe src="{}"></iframe>'.format(url))

You can use this:

# to read the lines
with open('file.txt', 'r') as f:
    strlist = f.readlines()
# list to store the URLs
webs = []
for x in strlist:
    webs.append(x.split(' ')[0])
print(webs)

In case if the URL position is not always at the beginning of the line. You can try

https?:\/\/www\.\w+\.com\/\w+\/id=(\d+)

then you can use back reference to get the URL and id.

Code example

with open('file.txt') as file:
for line in file:
    m = re.match(r'https?:\/\/www\.\w+\.com\/\w+\/id=(\d+)', line)
    if m:
        print("URL=%s" % m.group(0))
        print("ID=%d" % int(m.group(1)))

Demo

Related