Kit*_* Ho 5 python json dictionary
我收到了一个输出,它喜欢这个.
{
orange: '2',
apple: '1',
lemon: '3'
}
Run Code Online (Sandbox Code Playgroud)
我知道它不是标准的JSON格式,但是仍然可以解析为Python Dictionary类型吗?它是一个必须在橘子,苹果,柠檬,必须用引号引起来?
谢谢
phi*_*hag 14
这是有效的YAML(JSON的超集).使用PyYAML解析它:
>>> s = '''
... {
... orange: '2',
... apple: '1',
... lemon: '3'
... }'''
>>> import yaml
>>> yaml.load(s)
{'orange': '2', 'lemon': '3', 'apple': '1'}
Run Code Online (Sandbox Code Playgroud)
更多,因为字符串s中有一个制表空间,我们最好在解析为yaml之前删除它.
s=s.replace('\t','')
Run Code Online (Sandbox Code Playgroud)
否则,无法解析上面的字符串.
| 归档时间: |
|
| 查看次数: |
278 次 |
| 最近记录: |