Python Forum
Parse String between 2 Delimiters and add as single list items
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parse String between 2 Delimiters and add as single list items
#1
Hi all.

I`ve got a big string that contains multiple different data. The dataitems are seperated by {data}

I want to grab the data between theese 2 delimiters { and } and add them as a single item to another list.

i was using the code.
file = open('csdb.rss',mode='r')
 # read all lines at once
all_of_it = file.read()
data = []
data.append( re.findall(r'\{({^}}*)\}', all_of_it))
but this didnt work and added the whole content as just one list item. How should i call it parser correctly ? or as alternative read the file and parse it while reading and then append to the list as items ?

a sample sting is like :

{"name":"Freaky Fish DX +3PD [retailversion]","id":"202669","category":0,"group":"Onslaught","year":2021,"handle":"Shocker,Bieno64,Didi,Jazzcat","rating":0,"updated":"2021-04-07","released":"2021-04-06"},{"name":"Mimizuku Saga 10","id":"202614","category":0,"group":"Commocore","year":2021,"handle":"Bago Zonde","rating":0,"updated":"2021-04-07","released":"2021-04-05"},{"name":"Big Time Bugger","id":"202613","category":0,"group":"Commocore","year":2021,"handle":"Bago Zonde","event":"Cassette 50 Charity Competition","rating":0,"updated":"2021-04-05","released":"2021-03-30"}
Reply
#2
Except for not having brackets around it, this appears to be valid JSON. I'd add the brackets and parse as JSON. That will give you a list of dicts that you can read.

import json

file = open('csdb.rss', mode='r')
json_string = "[" + file.read().rstrip() + "]"
data = json.loads(json_string)
print(f"The first element is {data[0]}")

print("The keys and values in the first element are:")
for k, v in data[0].items():
    print(f"{k} => {v}")
Reply
#3
(Apr-11-2021, 09:41 PM)bowlofred Wrote: Except for not having brackets around it, this appears to be valid JSON. I'd add the brackets and parse as JSON. That will give you a list of dicts that you can read.

import json

file = open('csdb.rss', mode='r')
json_string = "[" + file.read().rstrip() + "]"
data = json.loads(json_string)
print(f"The first element is {data[0]}")

print("The keys and values in the first element are:")
for k, v in data[0].items():
    print(f"{k} => {v}")

ouch my Bad, i posted to the wring Subcategory. My python version is 2.7.18 There the Syntax is different and i get the error

File "F:\GP\C64\BBS\csdbrss\grabcsdb.py", line 34
print(f"The first element is {data[0]}")
^
SyntaxError: invalid syntax
Reply
#4
import re

text = '{"name":"Freaky Fish DX +3PD [retailversion]","id":"202669","category":0,"group":"Onslaught","year":2021,"handle":"Shocker,Bieno64,Didi,Jazzcat","rating":0,"updated":"2021-04-07","released":"2021-04-06"},{"name":"Mimizuku Saga 10","id":"202614","category":0,"group":"Commocore","year":2021,"handle":"Bago Zonde","rating":0,"updated":"2021-04-07","released":"2021-04-05"},{"name":"Big Time Bugger","id":"202613","category":0,"group":"Commocore","year":2021,"handle":"Bago Zonde","event":"Cassette 50 Charity Competition","rating":0,"updated":"2021-04-05","released":"2021-03-30"}'

results = re.findall('{(.+?)}', text)
for result in results:
    print(result)
Output:
"name":"Freaky Fish DX +3PD [retailversion]","id":"202669","category":0,"group":"Onslaught","year":2021,"handle":"Shocker,Bieno64,Didi,Jazzcat","rating":0,"updated":"2021-04-07","released":"2021-04-06" "name":"Mimizuku Saga 10","id":"202614","category":0,"group":"Commocore","year":2021,"handle":"Bago Zonde","rating":0,"updated":"2021-04-07","released":"2021-04-05" "name":"Big Time Bugger","id":"202613","category":0,"group":"Commocore","year":2021,"handle":"Bago Zonde","event":"Cassette 50 Charity Competition","rating":0,"updated":"2021-04-05","released":"2021-03-30"
Reply
#5
(Apr-11-2021, 09:52 PM)lastyle Wrote: ouch my Bad, i posted to the wring Subcategory. My python version is 2.7.18

2.7 won't have f-strings. Just change the print to use another format method.


for k, v in data[0].items():
    print(" => ".join((str(k), str(v))))
Reply
#6
(Apr-11-2021, 11:00 PM)bowlofred Wrote:
(Apr-11-2021, 09:52 PM)lastyle Wrote: ouch my Bad, i posted to the wring Subcategory. My python version is 2.7.18

2.7 won't have f-strings. Just change the print to use another format method.


for k, v in data[0].items():
    print(" => ".join((str(k), str(v))))

Both replies (approaches) work like a charm.

Thanks for a lot
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 76 Yesterday, 01:16 PM
Last Post: ann23fr
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,256 May-22-2023, 10:39 PM
Last Post: ICanIBB
  [split] Parse Nested JSON String in Python mmm07 4 1,413 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Finding combinations of list of items (30 or so) LynnS 1 836 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  Need help on how to include single quotes on data of variable string hani_hms 5 1,883 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  parse String jaykappy 2 714 Dec-23-2022, 07:42 AM
Last Post: praveencqr
  python sql query single quote in a string mg24 1 993 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  mutable values to string items? fozz 15 2,696 Aug-30-2022, 07:20 PM
Last Post: deanhystad
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,497 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  How to get list of exactly 10 items? Mark17 1 2,403 May-26-2022, 01:37 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

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