dict={'MENTION':'{'MENTION':'{'MENTION':'{'MENTION':'0','E':'10','M':'xxxx'}}
What should I do to show the xxxxx part of something like this?
python
'{"MENTIONEES":[{"S":"0", "E":"10", "M":"xxxx"}}'
The string is valid as both Python dictionary literal and JSON.
You need to understand where this string comes from and what format it follows and treat it in the correct way.
Since the existing answer from BLUEPIXY is to treat Python as literal, I will show you how to parse as JSON.
(Python has a class called dict
as the global variable dict
, so overwriting it is confusing.d
is used as the variable name below.)
#!/usr/bin/python3
# coding —utf-8
import json
d={'MENTION':'{'MENTION':'{'MENTION':'{'MENTION':'0','E':'10','M':'xx'}'}
# get the part of the JSON string from dictionary d
comment_json=d ['MENTION']
# Parse the JSON string and get a dictionary
comment_dict=json.loads(ment_json)
# obtain the desired value from the resulting dictionary
print(ment_dict["MENTIONONEES"][0]["M"])
The content of the maintenance is a string, so
You can't use it as a dictionary even if it's like a dictionary.
So
I think it's easy to convert using eval.
dict={'MENTION':'{'MENTION':'{'MENTION':'{'S':'0','E':'10','M':'xx'}'}
subdict=eval(dict['MENTION'])
print(subdict['MENTIONONEES'][0]['M'])
546 Understanding How to Configure Google API Key
547 Who developed the "avformat-59.dll" that comes with FFmpeg?
534 Uncaught (inpromise) Error on Electron: An object could not be cloned
542 Unable to install versioned in Google Colab
547 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.