我正在使用python为semanticnet / OpenGraphiti生成json。我在一个json对象中插入了RGBA坐标作为列表。
结果是每个RGBA点打印在一个单独的行上,我一直在手动修复,因为OpenGraphiti无法以这种格式解释它。
如何在对象内的单行返回坐标? (我不需要在一行上打印整个对象,只需要坐标。)
以下是代码示例以及我得到的结果:
color = []
for i in range(len(hostname)):
color.append([1.0, 1.0, 6.0, 1.0])
edges = zip(origin, hostname, connection, shortname, color)
Result:
{
"src": "oddjob",
"dst": "runloop",
"og:space:color": [
1.0,
1.0,
6.0,
1.0
],
"type": "connection",
"id": "oddjob-runloop"
}
这是我需要的结果:
{
"src": "oddjob",
"dst": "runloop",
"og:space:color": [1.0, 1.0, 6.0, 1.0],
"type": "connection",
"id": "oddjob-runloop"
}
非常感谢任何帮助!
答案 0 :(得分:0)
Python json
模块没有为您提供此级别的控制,但您可以半手动构建JSON:
import json
result = {
"src": "oddjob",
"dst": "runloop",
"og:space:color": [
1.0,
1.0,
6.0,
1.0
],
"type": "connection",
"id": "oddjob-runloop"
}
result = {k: json.dumps(v) for k, v in result.items()}
result = """{{
"src": {src},
"dst": {dst},
"og:space:color": {color},
"type": {type},
"id": {id}
}}""".format(color=result["og:space:color"], **result)
print(result)
输出:
{
"src": "oddjob",
"dst": "runloop",
"og:space:color": [1.0, 1.0, 6.0, 1.0],
"type": "connection",
"id": "oddjob-runloop"
}
或者您可以使用正则表达式来查找和替换数字数组。