正如标题所示,我目前正在从OpenCorporate API中提取一些JSON文本。然后,我将其放入一个数据框,然后将该数据框输出到CSV。
我遇到了CSV问题,例如,在“ previous_names”和“ industry_codes”下,我收到了更多嵌套的JSON文本。
即当我使用代码搜索“索尼”时,我在CSV的“以前的名称”列下收到了输出
[{'company_name': 'SONY DADC NEW MEDIA SOLUTIONS AUSTRALIA PTY LTD', 'start_date': '2015-03-10', 'end_date': '2017-07-02'}]
这是我的代码,
import requests
import pandas as pd
from pandas.io.json import json_normalize
count = int(input("Please enter the number of queries you wish to perform."))
iteration = 0
while iteration < count:
string = input("Please enter search terms.")
string.replace(" ", "+")
json_data = requests.get('https://api.opencorporates.com/companies/search?q=' + string).json()
df = None
for row in json_data["results"]["companies"]:
if df is None:
df = json_normalize(row["company"])
else:
df = pd.concat([df, json_normalize(row["company"])], sort=False)
string.replace("+", " ")
df.to_csv(string + '.csv', index=False, encoding='utf-8')
iteration += 1
print("Thank you, your queries have been performed.")
任何帮助将不胜感激!