Python Forum
extract substring from a string before a word !! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: extract substring from a string before a word !! (/thread-41081.html)



extract substring from a string before a word !! - evilcode1 - Nov-07-2023

hello all ...
this is my output :
Output:
{'status': True, 'msg': 'Success', 'data': [{'title': 'perrier'}, {'title': 'Polo'}, {'title': 'Purina'}, {'title': 'Pizza Hut'}, {'title': 'Pepsi'}, {'title': 'Pope1'}, {'title': 'Pantene'}, {'title': 'P&G'}, {'title': 'Pampers'}, {'title': 'Persil'}], 'html': '<ul class="list-group mt-1 mb-5"><li class="list-group-item">
i wanna extract them without ( 'html': '<ul class="list-group mt-1 mb-5"><li class="list-group-item">[/output] )
and then remove ( {'status': True, 'msg': 'Success', 'data': [{'title': ' ) and organize them is this way :

Output:
perrier Polo Purina Pizza Hut Pepsi Pope1 ...



RE: extract substring from a string before a word !! - Gribouillis - Nov-07-2023

If your object is a python dict D, just do
for item in D['data']:
    print(item['title'])



RE: extract substring from a string before a word !! - evilcode1 - Nov-07-2023

it;s a string and this is my code :
import re
 
string1 = '''{'status': True, 'msg': 'Success', 'data': [{'title': 'perrier'}, {'title': 'Polo'}, {'title': 'Purina'}, {'title': 'Pizza Hut'}, {'title': 'Pepsi'}, {'title': 'Pope1'}, {'title': 'Pantene'}, {'title': 'P&G'}, {'title': 'Pampers'}, {'title': 'Persil'}], 'html': '<ul class="list-group mt-1 mb-5"'''

requirement1 = re.search('data(.*)html', string1)
dd = requirement1.group(1)

test_str = ''.join(letter for letter in dd if letter.isalnum())
q = test_str.replace("title","")
print(q)
Output:
perrierPoloPurinaPizzaHutPepsiPope1PantenePGPampersPersil
how i can print each word on one line ?
Output:
perrier PoloPurina PizzaHut



RE: extract substring from a string before a word !! - evilcode1 - Nov-08-2023

i solve it :
 requirement1 = re.search('data(.*)html', str(q))
    dd = requirement1.group(1)

    test_str = ''.join(letter for letter in dd if letter.isalnum())
    q = test_str.replace("title","\n")
    print(q)
thank u all