После долгих поисков все направляли ответ на этот пост: [ Как объединить два словаря в одном выражении?
Но я хочу объединить два словаря и не сливаются. Оба моих словаря имеют один и тот же ключ, поэтому, когда я использую tempdict.update(data)
, он заменяет мои данные, а не добавляет их к нему. Словари выглядят так:
{
"symbol" : "AAPL",
"profile" : {
"price" : 254.29,
"beta" : "1.228499",
"volAvg" : "49330169",
"mktCap" : "1.11264072E12",
"lastDiv" : "2.92",
"range" : "170.27-327.85",
"changes" : -0.52,
"changesPercentage" : "(-0.20%)",
"companyName" : "Apple Inc.",
"exchange" : "Nasdaq Global Select",
"industry" : "Computer Hardware",
"website" : "http://www.apple.com",
"description" : "Apple Inc is designs, manufactures and markets mobile communication and media devices and personal computers, and sells a variety of related software, services, accessories, networking solutions and third-party digital content and applications.",
"ceo" : "Timothy D. Cook",
"sector" : "Technology",
"image" : "https://financialmodelingprep.com/images-New-jpg/AAPL.jpg"
}
}
{
"symbol" : "FB",
"profile" : {
"price" : 166.8,
"beta" : "1.062394",
"volAvg" : "21198830",
"mktCap" : "4.75455062E11",
"lastDiv" : "0",
"range" : "137.1-224.2",
"changes" : 0.85,
"changesPercentage" : "(+0.51%)",
"companyName" : "Facebook Inc.",
"exchange" : "Nasdaq Global Select",
"industry" : "Online Media",
"website" : "http://www.facebook.com",
"description" : "Facebook Inc is the world's largest online social network. Its products are Facebook, Instagram, Messenger, WhatsApp, and Oculus. Its products enable people to connect and share through mobile devices and personal computers.",
"ceo" : "Mark Zuckerberg",
"sector" : "Technology",
"image" : "https://financialmodelingprep.com/images-New-jpg/FB.jpg"
}
}
Я хочу, чтобы это выглядело так:
{
"companyProfiles" : [ {
"symbol" : "AAPL",
"profile" : {
"price" : 254.29,
"beta" : "1.228499",
"volAvg" : "49330169",
"mktCap" : "1.11264072E12",
"lastDiv" : "2.92",
"range" : "170.27-327.85",
"changes" : -0.52,
"changesPercentage" : "(-0.20%)",
"companyName" : "Apple Inc.",
"exchange" : "Nasdaq Global Select",
"industry" : "Computer Hardware",
"website" : "http://www.apple.com",
"description" : "Apple Inc is designs, manufactures and markets mobile communication and media devices and personal computers, and sells a variety of related software, services, accessories, networking solutions and third-party digital content and applications.",
"ceo" : "Timothy D. Cook",
"sector" : "Technology",
"image" : "https://financialmodelingprep.com/images-New-jpg/AAPL.jpg"
}
}, {
"symbol" : "FB",
"profile" : {
"price" : 166.8,
"beta" : "1.062394",
"volAvg" : "21198830",
"mktCap" : "4.75455062E11",
"lastDiv" : "0",
"range" : "137.1-224.2",
"changes" : 0.85,
"changesPercentage" : "(+0.51%)",
"companyName" : "Facebook Inc.",
"exchange" : "Nasdaq Global Select",
"industry" : "Online Media",
"website" : "http://www.facebook.com",
"description" : "Facebook Inc is the world's largest online social network. Its products are Facebook, Instagram, Messenger, WhatsApp, and Oculus. Its products enable people to connect and share through mobile devices and personal computers.",
"ceo" : "Mark Zuckerberg",
"sector" : "Technology",
"image" : "https://financialmodelingprep.com/images-New-jpg/FB.jpg"
}
} ]
}
Это то, что я получил до сих пор, но я знаю, что проблема в том, с .update
:
tempdict = dict()
for symbol in ["AAPL","FB"]:
with requests.get("https://financialmodelingprep.com/api/v3/company/profile/"+ symbol) as url:
data_string = url.content.decode()
data =json.loads(data_string)
tempdict.update(data)
print(tempdict)