Python 到 Json 的转换

标签 python json

我有一个将 excel 转换为 json 的脚本,并且有这段代码。

l3 = dict( zip(l1.split(','), l2.split(',')) )
print l3

其中 l1 和 l2 是列表。

输出是这样的

{'brand': 'car', 'color': 'red', 'model': '2009', 'value': '100000'}

是否可以删除这些键的单引号。

最佳答案

使用 json module从 Python 对象创建与 JSON 兼容的字符串:

>>> import json
>>> l3 = {'brand': 'car', 'color': 'red', 'model': '2009', 'value': '100000'}
>>> json.dumps(l3)
'{"model": "2009", "color": "red", "value": "100000", "brand": "car"}'

(或直接创建一个JSON文件):

>>> with open("test.json", "w") as j: json.dump(l3, j)

关于Python 到 Json 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20468884/

相关文章:

python - 使用 cx_Oracle 从 Python 连接到 Oracle 时出错

python - Matplotlib 3d 图 : how to get rid of the excessive white space?

json - 快速播放列表播放选定的歌曲

java - 如何使用 Spring application.properties 自定义 Jackson ObjectMapper?

php - 将数据推送到使用 json_encode 生成的 JSON

json - 在 JSON 规范中, "Since the first two characters of a JSON text will always be ASCII characters"是什么意思?

python - 我正在设置 python 类,但它打印 <main.deteil object at 0x03391d90

python - pandas:当索引不唯一时,使用 diff 和 groupby 时出现问题

sql - 如何限制列中的 JSON/JSONB 值完全不同?

python - 我想知道为什么魔法功能对特定运算符(operator)给出特定答案