【发布时间】:2020-01-30 06:01:41
【问题描述】:
我有 200 个文件我想用 Python 修改,命名为“1.json”、“2.json”、“3.json”……我正在尝试创建一个循环来打开和修改它们。我没有设法用“for i in range(1, 200):”来做到这一点,所以我尝试了以下方法。
myList = {"1.json", "2.json", "3.json"}
for toImport in myList:
with open("path1" + toImport) as f:
json_response = json.load(f)
for data in json_response:
try:
for special_issue in data["specific_issues"]:
for x in special_issue["bills_by_algo"]:
resulting_data.append(({"id": x["id"], "committees": x["committees"]}))
except KeyError as e:
print(e, "not found in entry.")
continue
b = pd.DataFrame(resulting_data)
print(b)
b.to_csv(r"path2" +toImport)
现在它不再发出错误消息,但文件没有导出...我应该更改什么?
【问题讨论】:
-
我已经更正了缩进错误并且它有效。但是我仍然无法导出文件...我在帖子中添加了代码行。