简体   繁体   English

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

[英]Python: How can I parse this list?

I am writing a script to grab stock info using an API.我正在编写一个脚本来使用 API 获取股票信息。 I have stored some API data in a variable called "ids" that I would like to loop through.我已经将一些 API 数据存储在一个名为“ids”的变量中,我想循环该变量。 When trying to loop using my code, it seems to output each character rather than each item in the list.当尝试使用我的代码循环时,似乎 output 每个字符而不是列表中的每个项目。 How can I correctly parse this list?如何正确解析此列表?

BTW, I noticed that the list has an additional set of square brackets surrounding it.顺便说一句,我注意到该列表周围有一组额外的方括号。 Not sure if this has anything to do with it.不确定这是否与它有关。 I am new to python and am not exactly sure what type of data I am dealing with.我是 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 4

5 5

0 0

...etc. ...ETC。

As @Chris mentioned it seems that you are iterating over a string.正如@Chris 提到的,您似乎正在迭代一个字符串。

Let

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"]]'

When you print it you receive:当您打印它时,您会收到:

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

If you use the json module to parse your data you can do the following:如果您使用 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: Don't use built-ins for variable names! PS:不要使用内置的变量名! https://docs.python.org/3/library/functions.html#id https://docs.python.org/3/library/functions.html#id

EDIT: The function robin_stocks.helper.request_get appears to have a parameter jsonify_data .编辑: function robin_stocks.helper.request_get似乎有一个参数jsonify_data When you use it, you should get the data in the format that you need当你使用它时,你应该得到你需要的格式的数据

I'm not positive I have the best answer here.我不肯定我在这里有最好的答案。 I noticed that running the following code works.我注意到运行以下代码有效。 I changed the variable names here.我在这里更改了变量名。 tempIDS is the same variable as ids was. tempIDSids是同一个变量。 I'm not sure if this is the best way, but it seems to function:我不确定这是否是最好的方法,但在 function 看来:

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)

Here is the full code so far to get a better context.这是迄今为止获得更好上下文的完整代码。 I know this probably could use a lot of improvements:我知道这可能需要很多改进:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM