Get individual strings between brackets

88 Views Asked by At

Let's say I have this string

[LEVEL]
    [NAME]The Girder Guide! [/NAME]
    [AUTHOR]draworigami[/AUTHOR]
    [AUTHORLEVEL]11[/AUTHORLEVEL]
    [COUNTRY]CA[/COUNTRY]
    [ID]62784[/ID]
    [RATING]4[/RATING]
    [DATE]2021-05-11 23:08:35[/DATE]
    [PLAYCOUNT]33[/PLAYCOUNT]
    [WINCOUNT]28[/WINCOUNT]
    [STARS]0[/STARS]
    [COMMENTS]1[/COMMENTS]
[/LEVEL]

Is there a way I can get the individual strings between each [] and [/]? I've kept taking shots at it with code from the internet to no avail.

3

There are 3 best solutions below

2
Ashok Arora On BEST ANSWER

This will return all the text between [] and [/]:

from bs4 import BeautifulSoup

rml = """
[LEVEL]
    [NAME]The Girder Guide! [/NAME]
    [AUTHOR]draworigami[/AUTHOR]
    [AUTHORLEVEL]11[/AUTHORLEVEL]
    [COUNTRY]CA[/COUNTRY]
    [ID]62784[/ID]
    [RATING]4[/RATING]
    [DATE]2021-05-11 23:08:35[/DATE]
    [PLAYCOUNT]33[/PLAYCOUNT]
    [WINCOUNT]28[/WINCOUNT]
    [STARS]0[/STARS]
    [COMMENTS]1[/COMMENTS]
[/LEVEL]
"""

html = rml.replace('[', '<').replace(']', '>')
soup = BeautifulSoup(html, 'html.parser')
print(soup.find('level').text)

Output:

The Girder Guide! 
draworigami
11
CA
62784
4
2021-05-11 23:08:35
33
28
0
1

Edit #1: The original string does not have newlines, so to pretty print:

rml = "[LEVEL][NAME]The Girder Guide![/NAME][AUTHOR]draworigami[/AUTHOR][AUTHORLEVEL]11[/AUTHORLEVEL][COUNTRY]CA[/COUNTRY][ID]62784[/ID][RATING]4[/RATING][DATE]2021-05-11 23:08:35[/DATE][PLAYCOUNT]33[/PLAYCOUNT][WINCOUNT]28[/WINCOUNT][STARS]0[/STARS][COMMENTS]1[/COMMENTS][/LEVEL]"

html = rml.replace('[', '<').replace(']', '>')
soup = BeautifulSoup(html, 'html.parser')
elements = soup.find('level').contents
for e in elements:
    print(e.text)
0
edusanketdk On

Try this:

st = "[LEVEL][NAME]The Girder Guide![/NAME][AUTHOR]draworigami[/AUTHOR][AUTHORLEVEL]11[/AUTHORLEVEL][COUNTRY]CA[/COUNTRY][ID]62784[/ID][RATING]4[/RATING][DATE]2021-05-11 23:08:35[/DATE][PLAYCOUNT]33[/PLAYCOUNT][WINCOUNT]28[/WINCOUNT][STARS]0[/STARS][COMMENTS]1[/COMMENTS][/LEVEL]"

st = st.split("]")
for i in range(len(st)):
    st[i] = st[i].replace("[", "")
    st[i]= st[i].replace("/", "")

st = st[:-1]

print(st)

The st becomes-

['LEVEL', 'NAME', 'The Girder Guide!NAME', 'AUTHOR', 'draworigamiAUTHOR', 'AUTHORLEVEL', '11AUTHORLEVEL', 'COUNTRY', 'CACOUNTRY', 'ID', '62784ID', 'RATING', '4RATING', 'DATE', '2021-05-11 23:08:35DATE', 'PLAYCOUNT', '33PLAYCOUNT', 'WINCOUNT', '28WINCOUNT', 'STARS', '0STARS', 'COMMENTS', '1COMMENTS', 'LEVEL']

What I did:

  • split the string around ] so a list of strings is obtained without the character ']'.
  • simply removed the characters [ and / individually from the strings in the list obtained.
  • skipped the last character because it was an empty string generated due to split.
0
AudioBubble On

How about using regular expression?

import re
s = '[LEVEL][NAME]The Girder Guide![/NAME][AUTHOR]draworigami[/AUTHOR][AUTHORLEVEL]11[/AUTHORLEVEL][COUNTRY]CA[/COUNTRY][ID]62784[/ID][RATING]4[/RATING][DATE]2021-05-11 23:08:35[/DATE][PLAYCOUNT]33[/PLAYCOUNT][WINCOUNT]28[/WINCOUNT][STARS]0[/STARS][COMMENTS]1[/COMMENTS][/LEVEL]'
s = s.replace('/', '')
result = []
for e in re.findall(r"\][A-Za-z0-9 _.:,!'/$\-]+\[", s):
    result.append(e.replace('[', '').replace(']', ''))

result

['The Girder Guide!',
 'draworigami',
 '11',
 'CA',
 '62784',
 '4',
 '2021-05-11 23:08:35',
 '33',
 '28',
 '0',
 '1']