Python:我如何解析这个列表?

2024-09-30 01:31:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个脚本来使用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.


Tags: 数据代码脚本api信息idids列表
2条回答

正如@Chris提到的,您似乎正在迭代一个字符串

id_list = '[["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"]]'

打印时,您将收到:

print(id_list)
>>> [["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"]]

如果使用json模块解析数据,可以执行以下操作:

import json
for ids in json.loads(id_list):
    for id_ in ids:
        print(id_)

>>> 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

PS:不要使用内置变量名https://docs.python.org/3/library/functions.html#id

编辑: 函数robin_stocks.helper.request_get似乎有一个参数jsonify_data。当您使用它时,您应该以您需要的格式获取数据

我不能肯定我有最好的答案。我注意到运行以下代码是有效的。我在这里更改了变量名tempIDS是与ids相同的变量。我不确定这是否是最好的方式,但它似乎发挥了作用:

tempIDS = r.stocks.get_news("AAPL","related_instruments")
base = "https://api.robinhood.com/instruments/"
global stockName
for tempID in tempIDS:
    for sID in tempID:
        print(sID)
        stockName = r.stocks.get_name_by_url(base+sID)

以下是迄今为止获得更好上下文的完整代码。我知道这可能需要很多改进:

import os
import json
import robin_stocks as r
#API Documentation: http://www.robin-stocks.com/en/latest/functions.html

#Login later for live trades
login = r.login('','')

global SYMBOL

def grabLatest(SYMBOL):
    global fundamentals,latestPrice,news,relatedInstruments,ids
    fundamentals = r.stocks.get_fundamentals(SYMBOL)
    latestPrice = r.stocks.get_latest_price(SYMBOL, includeExtendedHours=True)
    news = r.stocks.get_news(SYMBOL,"preview_text")
    tempIDS = r.stocks.get_news(SYMBOL,"related_instruments")
    
def sentimentAnalysis():
    global x
    global negativeScore
    global positiveScore
    global controversy 
    x = 0
    negativeScore = 0
    positiveScore = 0
    controversy = 0
    
    for i in news:
        custom_tweet = news[x]
        custom_tokens = remove_noise(word_tokenize(custom_tweet))
        sentiment = classifier.classify(dict([token, True] for token in custom_tokens))
        print(sentiment)
        print(custom_tweet)
        print("")
        x+=1
        if (sentiment is "Negative"):
            negativeScore -= 1
        if (sentiment is "Positive"):
            positiveScore += 1
    
    controversy = positiveScore - negativeScore
    
def parseRelated(SYMBOL):
    tempIDS = r.stocks.get_news(SYMBOL,"related_instruments")
    base = "https://api.robinhood.com/instruments/"
    global stockName
    global relatedStocks
    relatedStocks=[]
    for tempID in tempIDS:
        for sID in tempID:
            print(sID)
            relatedStocks.append(sID)
    
    for stock in relatedStocks:
        stockName = r.stocks.get_name_by_url(base+sID)
        print(stockName)
    
grabLatest("TSLA")
#parseRelated("TSLA")
print('FUNDAMENTALS:')
print(fundamentals[0])
print('')
print('LATEST PRICE:')
print(latestPrice)
print('')
sentimentAnalysis()
print("News sentiment analysis")     
print("Positive Score: ",positiveScore)
print("Negative Score: ",negativeScore)
print("Controversy: ",controversy)

相关问题 更多 >

    热门问题