Python 通过迭代形成JSON文件的正确方法
我需要在JSON文件中写入几个URL。 这就是我到目前为止所做的Python 通过迭代形成JSON文件的正确方法,python,json,Python,Json,我需要在JSON文件中写入几个URL。 这就是我到目前为止所做的 for index, document in enumerate(master_data): # create a dictionary for each document in the master list document_dict = {} document_dict['cik_number'] = document[0]
for index, document in enumerate(master_data):
# create a dictionary for each document in the master list
document_dict = {}
document_dict['cik_number'] = document[0]
document_dict['company_name'] = document[1]
document_dict['form_id'] = document[2]
document_dict['date'] = document[3]
document_dict['file_url'] = document[4]
master_data[index] = document_dict
for document_dict in master_data:
# if it's a 10-K document pull the url and the name.
if document_dict['form_id'] == '10-K':
# get the components
data = {}
data['company name'] = document_dict['company_name']
data['cik_number'] = document_dict['cik_number']
data['form_id'] = document_dict['form_id']
data['date'] = document_dict['date']
data['file_url'] = document_dict['file_url']
write(data, JSON_file_to_write)
JSON_file_to_write.write('\n')
这就是我最后得到的
{"company name": "ZERO CORP", "cik_number": "109284", "form_id": "10-K", "date": "19940629", "file_url": "https://www.sec.gov/Archives/data/109284/0000898430-94-000468.txt"}
{"company name": "FOREST LABORATORIES INC", "cik_number": "109563", "form_id": "10-K", "date": "19940628", "file_url": "https://www.sec.gov/Archives/data/38074/0000038074-94-000021.txt"}
{"company name": "GOULDS PUMPS INC", "cik_number": "14637", "form_id": "10-K", "date": "19940331", "file_url": "https://www.sec.gov/Archives/data/42791/0000042791-94-000002.txt"}
{"company name": "GENERAL HOST CORP", "cik_number": "275605", "form_id": "10-Q", "date": "19940701", "file_url": "https://www.sec.gov/Archives/data/40638/0000950124-94-001209.txt"}
但据我所知,我创建了一个包含JSON文件的文本文件。
但是我需要创建一个JSON文件,这样我就可以迭代这些文件的url并将它们下载到一个文件夹中
实际上,我对Python、JSON和编码非常陌生,我正试图完成我的Capstone项目的任务,因此,如果我以错误的格式提出问题,请不要太苛刻,请告诉我正确的方式,以便我可以学习,并与此门户中的每个人进行更好的沟通 您希望将其附加到列表中,而不是立即写入文件。然后将dict列表转换为JSON对象,并将其写入文件
import json
jsonList = []
for document_dict in master_data:
# if it's a 10-K document pull the url and the name.
if document_dict['form_id'] == '10-K':
# get the components
data = {}
data['company name'] = document_dict['company_name']
data['cik_number'] = document_dict['cik_number']
data['form_id'] = document_dict['form_id']
data['date'] = document_dict['date']
data['file_url'] = document_dict['file_url']
jsonList.append(data)
with open('filename.txt', 'w') as the_file: #change w to a if you want to append
the_file.write(json.dumps(jsonList))
您希望将其附加到列表中,而不是立即写入文件。然后将dict列表转换为JSON对象,并将其写入文件
import json
jsonList = []
for document_dict in master_data:
# if it's a 10-K document pull the url and the name.
if document_dict['form_id'] == '10-K':
# get the components
data = {}
data['company name'] = document_dict['company_name']
data['cik_number'] = document_dict['cik_number']
data['form_id'] = document_dict['form_id']
data['date'] = document_dict['date']
data['file_url'] = document_dict['file_url']
jsonList.append(data)
with open('filename.txt', 'w') as the_file: #change w to a if you want to append
the_file.write(json.dumps(jsonList))
在您的示例中,您已经创建了4个单独的词典。你应该把那4本字典列在一个单子里。然后将其转换为JSON字符串,然后将该字符串写入文件。你应该把那4本字典列在一个单子里。然后将其转换为JSON字符串,然后将该字符串写入文件。非常感谢!你认为我问问题的方式可以吗?或者我应该试着简化它?我想成为专业的提问者:)在我看来这很好。我解释了你的观点。有正确的标签。发布您的输出。大多数人都希望看到预期的输出,但从你发布的内容不难看出。非常感谢!你认为我问问题的方式可以吗?或者我应该试着简化它?我想成为专业的提问者:)在我看来这很好。我解释了你的观点。有正确的标签。发布您的输出。大多数人都希望看到预期的输出,但从你发布的内容不难看出。