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