我正在编写一个脚本来使用API获取股票信息。我已经在一个名为“ids”的变量中存储了一些API数据,我想循环使用它。当尝试使用我的代码循环时,它似乎输出每个字符,而不是列表中的每个项目。如何正确解析此列表
顺便说一句,我注意到列表周围还有一组附加的方括号。不确定这是否与此有关。我是python新手,不确定我处理的是哪种类型的数据
print(ids)
[["450dfc6d-5510-4d40-abfb-f633b7d9be3e", "943c5009-a0bb-4665-8cf4-a95dab5874e4"], ["450dfc6d-5510-4d40-abfb-f633b7d9be3e", "943c5009-a0bb-4665-8cf4-a95dab5874e4"], ["450dfc6d-5510-4d40-abfb-f633b7d9be3e", "41eac3c6-f7f7-4c4a-b696-ab9d1b913981"], ["450dfc6d-5510-4d40-abfb-f633b7d9be3e"], ["450dfc6d-5510-4d40-abfb-f633b7d9be3e"], ["450dfc6d-5510-4d40-abfb-f633b7d9be3e"], ["943c5009-a0bb-4665-8cf4-a95dab5874e4", "450dfc6d-5510-4d40-abfb-f633b7d9be3e"], ["450dfc6d-5510-4d40-abfb-f633b7d9be3e", "943c5009-a0bb-4665-8cf4-a95dab5874e4"], ["450dfc6d-5510-4d40-abfb-f633b7d9be3e", "943c5009-a0bb-4665-8cf4-a95dab5874e4"], ["450dfc6d-5510-4d40-abfb-f633b7d9be3e"]]
for id in ids:
print(id)
[
[
"
4
5
0
...etc.
正如@Chris提到的,您似乎正在迭代一个字符串
让
打印时,您将收到:
如果使用json模块解析数据,可以执行以下操作:
PS:不要使用内置变量名https://docs.python.org/3/library/functions.html#id
编辑: 函数
robin_stocks.helper.request_get
似乎有一个参数jsonify_data
。当您使用它时,您应该以您需要的格式获取数据我不能肯定我有最好的答案。我注意到运行以下代码是有效的。我在这里更改了变量名
tempIDS
是与ids
相同的变量。我不确定这是否是最好的方式,但它似乎发挥了作用:以下是迄今为止获得更好上下文的完整代码。我知道这可能需要很多改进:
相关问题 更多 >
编程相关推荐