with open('test.json', 'r') as file:
# Get text without whitespace or newlines
text = file.read().replace(' ', '').replace('\n', '')
# Split by '{', discard first entry (will be empty)
objects = text.split('{')[1:]
for object in objects:
# Add the split delimiter back
object = '{' + object
# Get the id relative to the json data
id = object[ object.find('"ID"') + 6 :
object.find('"Name"') - 2 ]
# Add the file extension
id += '.json'
# If the file doesnt exist, create it and write the data
with open(id, 'x') as file:
file.write(object)
我不知道为什么你的json是无效的,因为你用“NOT”分隔逗号作为要求,我希望这一条能说明你的问题。在
假设您的JSON数据正确验证如下:
可以用JSON Formatter and Validator验证。在
您可以从^{} 提取每个JSON对象,并用^{} 将每个对象写入一个单独的文件:
^{pr2}$将生成以下JSON文件:
349878.json:
349879.json:
349880.json:
你可以用结构分裂()并使用切片查找id并创建文件。如果不删除空白,可以使用不同的索引。在
相关问题 更多 >
编程相关推荐