2

I have files with data looking like

{u'session_id': u'6a208c8cfada4048b26ea7811cbac20f'}

That is, key value pairs and arrays of objects with key value pairs which are of the form u'key' : u'value'

More specifically, the files I see look like what one gets after calling json.loads()on a JSON file.

I want to some how get the data present in these files as python objects or at least valid JSON format (some thing like reverse of json.loads()) so that I can do something like obj['session_id'] and get "6a208c8cfada4048b26ea7811cbac20f".

Thanks in advance

1 Answer 1

3

You can use literal_eval from the ast module, which is better than using eval directly:

>>> ast.literal_eval("{u'session_id': u'6a208c8cfada4048b26ea7811cbac20f'}")['session_id']
u'6a208c8cfada4048b26ea7811cbac20f'
>>> z = ast.literal_eval("{u'session_id': u'6a208c8cfada4048b26ea7811cbac20f'}")
>>> isinstance(z, dict)
True
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.