python - Python缩进字符串到JSON
问题描述
在Python
我有一个格式为:
header:
hello: world
world: hello
this:
is: difficult
this: 2
我想转换成JSON
:
{ header: { hello: world, world: hello, this: { is: difficult} }}
这可能以一种有效的方式吗?
我试过了,yaml.load(str)
但不能处理。
更新
我已经用空格替换了制表符,这yaml
不会失败。
我的最后一个问题是,在我的原始字符串/对象中,我可以有重复的键。不知道如何处理这个问题yaml
,但它的顺序是随机的,所以我想保留所有键,或者将包含对象的键作为值优先于包含简单整数的键。有道理,有什么办法处理吗?
解决方案
YAML 对我来说很好用......
test="""header:
hello: world
world: hello
this:
is: difficult"""
print(test)
'标题:\n你好:世界\n世界:你好\n这个:\n是:困难'
import yaml
yaml.load(test)
{'header': {'hello': 'world', 'world': 'hello', 'this': {'is': '困难'}}}
推荐阅读
- c# - 调用函数以在 ASP.NET Core 中定期更新数据库值?
- python - 如何在 Django 模板页面中设置 href
- c++ - CGAL:在探索平面地图时使用“标记”类型
- c - 无法编译 C 代码,因为 ip_icmp.h 使用类型“u_int32_t”和“n_short”
- javascript - Document.getElementsByClassName() 的复杂性?
- python - 如何让 pyautogui 在 google 中输入链接并循环用户决定的次数?
- javascript - babel core-js typed-array-constructor:找不到模块'../internals/to-offset'
- ios - 在 Swift 中创建像在 Ada 中一样的聊天
- java - 获取 java.sql.SQLRecoverableException:尝试从 jdbc 驱动程序获取序列下一个值时,不再从套接字读取数据
- ios - 如何强制单元格行更新高度