gpt4 book ai didi

python - 具有多个键和多个属性的 JSON 类

转载 作者:太空宇宙 更新时间:2023-11-04 04:12:35 25 4
gpt4 key购买 nike

我目前正在尝试弄清楚如何在 Python 中解析 JSON 信息,但比我想象的要困难得多。

下面是我尝试解析的信息示例。

{
"2019-05-09": {
"1. open": "124.2900",
"2. high": "125.7800",
"3. low": "123.5700",
"4. close": "125.5000",
"5. volume": "23491093"
},
"2019-05-08": {
"1. open": "125.4400",
"2. high": "126.3700",
"3. low": "124.7500",
"4. close": "125.5100",
"5. volume": "25775583"
},
"2019-05-07": {
"1. open": "126.4600",
"2. high": "127.1800",
"3. low": "124.2200",
"4. close": "125.5200",
"5. volume": "36017661"
}
}

我试图将每一天存储到一个类中,以便我可以解析信息。

在下面的示例中,我只是尝试打印这些记录的开头。根据我看过的简单示例,这应该可行,但总是会出现错误“字符串索引必须是整数”。

from alpha_vantage.timeseries import TimeSeries
import json

class day_history:
def __init__(self, date, open, high, low, close, volume):
self.date = date
self.open = open
self.high = high
self.low = low
self.close = close
self.volume = volume

alpha_adv_key = "aaaaaaaaaaa"

ts = TimeSeries(key=alpha_adv_key)
data, meta_data = ts.get_daily(symbol='MSFT')

results = json.dumps(data)

for day in results:
print(day["1. open"])

解析此 JSON 数据以便将其存储在类中的正确方法是什么?

最佳答案

如果你想把它变成一个类,请看下面我的代码。

话虽如此,我认为您遇到的问题是for day in results:。这将返回每个键(即一个字符串)。我认为您打算在 results.values() 中执行 for day:,这将返回所有词典。

将json转化为类。

您可以使用 ** 解包的魔力。我将您的输入用作参数 d

days = []
for day, info in d.items():
temp = info.copy() # A copy because we don't want to change the original
keys = list(temp.keys()) # List of all keys (Ex: 1. open)

# We want to remove the beginning number so we just have
# the wanted attribute name(Ex: open instead of 1. open)
for key in keys:
temp[key[3:]] = temp.pop(key)
temp['date'] = day # Add our date to the temp dictionary

# note the ** notation. This unpacks a dictionary to keyword arguments
# so it would be like passing: (date=day, open=..., close=..., ...)
# that is why I had to remove the numbers before the variable name.
days.append(day_history(**temp)) # Pass this new dict to our constructor

# This will print out all the days
print(days)

我在你的类中添加了一个 __repr__ 方法来打印它们:

    def __repr__(self):
return str(self.date) + str(self.high)

如果您不需要更多的类方法,您可能只想考虑使用 namedtuple:

from collections import namedtuple

Day_History = namedtuple("Day_History", "date open high low close volume")
days = []

for day, info in d.items():
temp = info.copy()
keys = list(temp.keys())
for key in keys:
temp[key[3:]] = temp.pop(key)
temp['date'] = day
days.append(Day_History(**temp))

for day in days:
print(day)

输出:

Day_History(date='2019-05-09', open='124.2900', high='125.7800', low='123.5700', close='125.5000', volume='23491093')
Day_History(date='2019-05-08', open='125.4400', high='126.3700', low='124.7500', close='125.5100', volume='25775583')
Day_History(date='2019-05-07', open='126.4600', high='127.1800', low='124.2200', close='125.5200', volume='36017661')

并且您不能通过执行以下操作来打印 namedtuple 参数:

for day in days:
print(day.open)

关于python - 具有多个键和多个属性的 JSON 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56080500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com