'Extract course and image_50x50 from a txt file using re method in Python

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":(.*)') 


Solution 1:[1]

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))

Solution 2:[2]

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"]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Denrix