【发布时间】:2020-06-19 01:59:24
【问题描述】:
我有一个这样的字符串。如何创建一个字典,其中 First-tags 作为键,之后的所有内容:作为值?
test_string = """###Some Comment
First-tags :
{
"tag1": {
"tagKey1": "tagValue1",
"tagKey2": "tagValue2"
},
"tag2": {
"tagKey1": "tagValue1",
"tagKey2": "tagValue2"
}
so on .....
}
"""
示例: 键将是 First-tags 和价值将是
{
"tag1": {
"tagKey1": "tagValue1",
"tagKey2": "tagValue2"
},
"tag2": {
"tagKey1": "tagValue1",
"tagKey2": "tagValue2"
}
so on .....
}
[编辑:字符串数据在文件中。问题是从文件中读取并创建一个字典,其中键是注释,值是 Json 数据]
例如,文件将有:
###Some Comment
First-tags :
{
"tag1": {
"tagKey1": "tagValue1",
"tagKey2": "tagValue2"
},
"tag2": {
"tagKey1": "tagValue1",
"tagKey2": "tagValue2"
}
so on .....
}
###2nd Comment
Second-tags :
{
"tag1": {
"tagKey1": "tagValue1",
"tagKey2": "tagValue2"
},
"tag2": {
"tagKey1": "tagValue1",
"tagKey2": "tagValue2"
}
so on .....
}
###Some other Comment
someother-tags :
{
"tag1": {
"tagKey1": "tagValue1",
"tagKey2": "tagValue2"
},
"tag2": {
"tagKey1": "tagValue1",
"tagKey2": "tagValue2"
}
so on .....
}
【问题讨论】:
-
您是在问如何将包含 cmets 的
json字符串解析到字典中?这似乎比这更复杂一些;字符串是否总是包含First-tags? -
您是在问如何将包含 cmets 的 json 字符串解析到字典中?是的,但文件结构将是这样的: cmets JSON data 另一个注释 JSON data 等等... First-tags 只是一个示例。它可以是任何东西。
标签: python json python-3.x dictionary