我如何用python对一个字典进行JSON编码?

2024-09-22 16:23:33 发布

您现在位置:Python中文网/ 问答频道 /正文

value = re.findall(b,'some regex',respdata)
keywords = re.findall(b,'some regex',respdata)

#Empty dictionary
Dictionary = {}
x=0
for eachValue in value:
        Dictionary[eachValue] = keyword[x]
        x+=1        
print(Dictionary)

在这之后,我想用JSON对字典进行编码“值:关键字“格式,有什么建议吗?提前谢谢你


Tags: inrefordictionaryvaluesomekeywordregex
2条回答

只需使用stdlib:

import json
print(json.dumps(Dictionary))

另外,请确保在变量命名时遵循PEP约定,我认为字典变量的大写是违反的,请参见:https://www.python.org/dev/peps/pep-0008/#prescriptive-naming-conventions

Python有一个模块json

value = re.findall('some regex',respdata)
keywords = re.findall('some regex',respdata)
print json.dumps(dict(zip(value, keywords)))

相关问题 更多 >