我是python新手。我想创建一个新数组来保存JSON数据的每个部分的类型。再次检查,如果任何部分在该数组中有一个类型,则将其保存在另一个文件中,称为“type0.txt”、“type1.txt”。这是我的密码。在
from __future__ import print_function
import json
# Remember to check the path to articles.json relative to this file before executing
with open('articles.json') as json_data:
# Load JSON
articles = json.load(json_data)
print(len(articles), "Articles loaded succesfully")
# Loop through every article in the json file
typeLabel = []
for article in articles:
typeL = article["type"]
typeLabel.append(typeL)
typeLabel = set(typeLabel)
typeLabel = list(set(typeLabel))
seen = set()
resultType = []
for item in typeLabel:
if item not in seen:
seen.add(item)
resultType.append(item)
for article in articles
x = 0
for i in range(0,len(resultType) - 1):
if article["type"] = resultType[i]:
filePathName = resultType[i] + x + '.txt'
with open(filePathName, 'w') as reader:
json.dump(article["content"], reader)
x += 1
pass
首先我得到一个错误
for article in articles ^ SyntaxError: invalid syntax
然后我不确定我写的其余的语法是否正确,所以你能帮我修改我的代码吗。非常感谢!!在
你忘了结肠。在
@评论 您正在尝试将不同类型连接在一起。您应该改用字符串格式。format方法将在幕后找到不同对象的字符串表示。在
^{pr2}$而不是
相关问题 更多 >
编程相关推荐