Kyle Dixon
Kyle Dixon

Reputation: 494

Python: How can I parse this list?

I am writing a script to grab stock info using an API. I have stored some API data in a variable called "ids" that I would like to loop through. When trying to loop using my code, it seems to output each character rather than each item in the list. 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.

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.

Upvotes: 0

Views: 130

Answers (2)

borisdonchev
borisdonchev

Reputation: 1224

As @Chris mentioned it seems that you are iterating over a string.

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:

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! https://docs.python.org/3/library/functions.html#id

EDIT: The function robin_stocks.helper.request_get appears to have a parameter jsonify_data. When you use it, you should get the data in the format that you need

Upvotes: 2

Kyle Dixon
Kyle Dixon

Reputation: 494

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. I'm not sure if this is the best way, but it seems to 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)

Upvotes: 0

Related Questions