2 回答
TA贡献1818条经验 获得超7个赞
您可以使用json.dumps获取 json 字符串作为输出。
import json
TRAIN_DATA = [
("Who is Shaka Khan?", {"entities": [(7, 17, "PERSON")]}),
("I like London and Berlin.", {"entities": [(7, 13, "LOC"), (18, 24, "LOC")]}),
]
print json.dumps(TRAIN_DATA)
输出:
[["Who is Shaka Khan?", {"entities": [[7, 17, "PERSON"]]}], ["I like London and Berlin.", {"entities": [[7, 13, "LOC"], [18, 24, "LOC"]]}]]
您可以类似地使用json.loads()在 python 列表中加载 json 字符串
例子
json.loads('[["Who is Shaka Khan?", {"entities": [[7, 17, "PERSON"]]}], ["I like London and Berlin.", {"entities": [[7, 13, "LOC"], [18, 24, "LOC"]]}]]')
TA贡献1772条经验 获得超6个赞
您可以尝试以下操作:
import json
with open('data.json', 'w') as outfile:
json.dump(TRAIN_DATA, outfile)
添加回答
举报