0

I have the following information in the txt file:

        "id":12,
     }
  ],

     {
        "id":1254578,

           {
           }
        ]

I tried splitting item: from the unit: using

course_id = re.search(' "id":(.*)') 
Tech
  • 65
  • 7
  • You data is in JSON format, so you should first load the data using the json library. Then you will have a Python Dictionary that will enable you to select any Data item. – Hezi Shahmoon Apr 24 '22 at 08:38
  • I've used Jason formatter on the attached pic, but how do I access the "id": information? I tried splitting but no luck – Tech Apr 24 '22 at 08:43
  • which `id`? There are 2 in `items` – cards Apr 24 '22 at 09:04
  • The first one with the course. However, I'd love to know how to extract the second one too, probably in a separate list? – Tech Apr 24 '22 at 09:12

2 Answers2

0

There's no need to use regex on the string

Just use python's json package to turn your data variable into a dict

Something like this

import json

course_data = json.loads(data)
course_id = course_data["unit"]["items"]["id"]
Denrix
  • 1
  • 3
  • Thank you, but on my exercise, its asked not to use jason unfortunately. Either re or str – Tech Apr 24 '22 at 08:59
0

Not the best regex but they work. Used re.S for dot all.

import re

text = ''
with open('my file.txt', 'r') as fd:
    text += fd.read()

# first id
m1 = re.search(r'type.+?"id":(\d+)', text, re.S)
print(m1.group(1))

# second id
m2 = re.search(r'_class.+?"id":(\d+)', text, re.S)
print(m2.group(1))
cards
  • 3,936
  • 1
  • 7
  • 25
  • But how do I put data in triple quotes? It's a raw.txt file and I'm not allowed to edit it. I simply used Jason formater to get the above one-line sample code. Appreciate your help – Tech Apr 24 '22 at 10:33
  • @Tech ah sorry! I forgot that the text come from a file, I edit the answer – cards Apr 24 '22 at 10:35
  • You're a legend Thanks heaps – Tech Apr 24 '22 at 10:45