Python Forum
extract substring from a string before a word !!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extract substring from a string before a word !!
#1
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 ...
Reply
#2
If your object is a python dict D, just do
for item in D['data']:
    print(item['title'])
Reply
#3
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
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Retrieve word from string knob 4 502 Jan-22-2024, 06:40 PM
Last Post: Pedroski55
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,076 Apr-08-2023, 06:36 PM
Last Post: Winfried
  extract last word from path. mg24 4 1,761 Nov-01-2022, 10:55 AM
Last Post: carecavoador
  ValueError: substring not found nby2001 4 7,964 Aug-08-2022, 11:16 AM
Last Post: rob101
  Match substring using regex Pavel_47 6 1,446 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  Isolate a word from a long string nicocorico 2 1,545 Feb-25-2022, 01:12 PM
Last Post: nicocorico
  Substring Counting shelbyahn 4 6,154 Jan-13-2022, 10:08 AM
Last Post: krisputas
  Extract a string between 2 words from a text file OscarBoots 2 1,885 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  change string in MS word Mr_Blue 8 3,353 Sep-19-2021, 02:13 PM
Last Post: snippsat
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,518 Aug-12-2021, 04:25 PM
Last Post: palladium

Forum Jump:

User Panel Messages

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