0

I am trying to retrieve variables from a string in python and having a tough time.

String looks something like this:

"{'_id': ObjectId('636d855390928b1b0392b764'), 'request_id': 99, 'timestamp': '2022-11-10T18:12:19.273576'}"

I want to retrieve the following values and assign them to variables:

objID = 636d855390928b1b0392b764

reqID = 99

timestamp = 2022-11-10T18:12:19.273576

The string will follow this format but obviously the values are random. I have tried using the import re library but maybe because of the '' characters I don't get the correct results.

I tried

objectID= re.findall(r'\'.*?\'', s)

to test out how this works this gives me everything between '' in a list. However, I want to get a value between ObjectId(' I attempted the following:

re.findall(r'ObjectId(.+?)',s)

But this outputs (. I try to get the '' marks in but I always get an error no matter how I try to include them.

Edit: included attempted code

0 Answers0