Python Forum

Full Version: extract substring from a string before a word !!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 ...
If your object is a python dict D, just do
for item in D['data']:
    print(item['title'])
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
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