Python: How i Can print only a particular string starting with a specific character?

923 Views Asked by At

I have a text file which contains list of urls which looks like as follows:

https://www.ebay.com/itm/Egyptian-Comfort-1800-Count-4-Piece-Bed-Sheet-Set-Deep-Pocket-Bed-Sheets/142436469971?epid=1760442729&hash=item2129e00cd3%3Ag%3A7gIAAOSw3YBdRVJd&_trkparms=%2526rpp_cid%253D601435485fceeb223c6f4511&var=442541824291

Here i only want to print epid=1760442729 while reading the text file.

I have tried:

result = []`
with open('deals.txt', 'r') as f:
for line in f:
    if line.startswith('?epid='):
        break
    result.append(line)
print(result[0].split('epid='))

But i am not getting as expected result.

Any help or suggestions will be helpful for me. Thanks in advance

3

There are 3 best solutions below

1
On BEST ANSWER
import re

s = """https://www.ebay.com/itm/Egyptian-Comfort-1800-Count-4-Piece-Bed-Sheet-Set-Deep-Pocket-Bed-Sheets/142436469971?epid=1760442729&hash=item2129e00cd3%3Ag%3A7gIAAOSw3YBdRVJd&_trkparms=%2526rpp_cid%253D601435485fceeb223c6f4511&var=442541824291
https://www.ebay.com/itm/Egyptian-Comfort-1800-Count-4-Piece-Bed-Sheet-Set-Deep-Pocket-Bed-Sheets/142436469971?epid=172442729&hash=item2129e00cd3%3Ag%3A7gIAAOSw3YBdRVJd&_trkparms=%2526rpp_cid%253D601435485fceeb223c6f4511&var=442541824291"""

for i in re.findall(r'epid=(\d+)&', s, re.MULTILINE):
    print(f'epid = {i}')
epid = 1760442729
epid = 172442729
1
On

I would do it with a substring if the same structure is always used

result = []
with open('deals.txt', 'r') as f:
for line in f:
    a= line.find('epid=')
    b= line.find('&hash=')
    print(line[a:b])
    result.append(line)
0
On

Use the library that was designed to parse URL.

Example:

from urllib.parse import urlparse, parse_qs

URL='https://www.ebay.com/itm/Egyptian-Comfort-1800-Count-4-Piece-Bed-Sheet-Set-Deep-Pocket-Bed-Sheets/142436469971?epid=1760442729&hash=item2129e00cd3%3Ag%3A7gIAAOSw3YBdRVJd&_trkparms=%2526rpp_cid%253D601435485fceeb223c6f4511&var=442541824291'

url_component = urlparse(URL)
query_component = parse_qs(url_component.query)
epid_data = query_component['epid'][0]
print(f'epid = {epid_data}')