I am trying to extract information from a list within a list within a list to end up with something like this from the information below: ('h': '0.77584', 'l': '0.77292'), ('h': '0.77521', 'l': '0.77206')
print(dict)
[{'complete': True, 'volume': 2290, 'time': '2021-01-15', 'mid': {'o': '0.77540', 'h': '0.77584', 'l': '0.77292', 'c': '0.77440'}}, {'complete': True, 'volume': 2312, 'time': '2021-01-15', 'mid': {'o': '0.77436', 'h': '0.77521', 'l': '0.77206', 'c': '0.77206'}}]
Not sure how to go about it. I tried
something = list(list(dict.items())[0].items())[3][1]
print(something)
However, this returned {'o': '0.77540', 'h': '0.77584', 'l': '0.77292', 'c': '0.77440'}
How to get the requested data?
You can use the following list and dict comprehension
dict = [{'complete': True, 'volume': 2290, 'time': '2021-01-15', 'mid': {'o': '0.77540', 'h': '0.77584', 'l': '0.77292', 'c': '0.77440'}}, {'complete': True, 'volume': 2312, 'time': '2021-01-15', 'mid': {'o': '0.77436', 'h': '0.77521', 'l': '0.77206', 'c': '0.77206'}}]
res = [{k:v for k, v in i['mid'].items() if k in 'hl'} for i in dict]
print(res)
Output
[{'h': '0.77584', 'l': '0.77292'}, {'h': '0.77521', 'l': '0.77206'}]
First you can take the mid child from each parent dictionary in the list.
dict = [{'complete': True, 'volume': 2290, 'time': '2021-01-15', 'mid': {'o': '0.77540', 'h': '0.77584', 'l': '0.77292', 'c': '0.77440'}}, {'complete': True, 'volume': 2312, 'time': '2021-01-15', 'mid': {'o': '0.77436', 'h': '0.77521', 'l': '0.77206', 'c': '0.77206'}}]
old_dict = [dict[0]['mid'],dict[1]['mid']]
Then loop through those entries and filter based off the keys (i.e. remove o and c):
list_you_want = []
for i in [0,1]:
list_you_want.append({ your_key: old_dict[i][your_key] for your_key in ['h','l'] })
Which gives you:
[{'h': '0.77584', 'l': '0.77292'}, {'h': '0.77521', 'l': '0.77206'}]
Related
i have two(n) websocket messages input like this, separated by "==new msg=="
(don't know why but some of the messages are longer..)
++Rcv raw: b'\x81~\x02\xff{"data":[{"c":null,"p":20926.99,"s":"BINANCE:BTCUSDT","t":1656017217026,"v":0.0008},{"c":null,"p":20927.45,"s":"BINANCE:BTCUSDT","t":1656017217056,"v":0.0005},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.001},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.00061},{"c":null,"p":20930.52,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.1},{"c":null,"p":20931.18,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.05098},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03654},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.03},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.01882}],"type":"trade"}'
++Rcv decoded: fin=1 opcode=1 data=b'{"data":[{"c":null,"p":20926.99,"s":"BINANCE:BTCUSDT","t":1656017217026,"v":0.0008},{"c":null,"p":20927.45,"s":"BINANCE:BTCUSDT","t":1656017217056,"v":0.0005},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.001},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.00061},{"c":null,"p":20930.52,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.1},{"c":null,"p":20931.18,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.05098},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03654},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.03},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.01882}],"type":"trade"}'
==new msg==
++Rcv raw: b'\x81~\x03\x03{"data":[{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.08477},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.1},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.25},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.07053},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217694,"v":0.08207},{"c":null,"p":20926.98,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.05098},{"c":null,"p":20926.31,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.1},{"c":null,"p":20925.5,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.16424},{"c":null,"p":20927.87,"s":"BINANCE:BTCUSDT","t":1656017217711,"v":0.00052},{"c":null,"p":20923.16,"s":"BINANCE:BTCUSDT","t":1656017217927,"v":0.0155}],"type":"trade"}'
++Rcv decoded: fin=1 opcode=1 data=b'{"data":[{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.08477},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.1},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.25},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.07053},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217694,"v":0.08207},{"c":null,"p":20926.98,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.05098},{"c":null,"p":20926.31,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.1},{"c":null,"p":20925.5,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.16424},{"c":null,"p":20927.87,"s":"BINANCE:BTCUSDT","t":1656017217711,"v":0.00052},{"c":null,"p":20923.16,"s":"BINANCE:BTCUSDT","t":1656017217927,"v":0.0155}],"type":"trade"}'
==new msg==
and i have some code so far
import websocket
import json
from collections import ChainMap
y = []
c = []
def on_message(ws, message):
print("==new msg==")
y.append(json.loads(message))
def on_error(ws, error):
print(error)
def on_close(ws,close_status_code, close_reason):
print("==closed==")
print(len(y))
c = dict(ChainMap(*y))
print(c)
def on_open(ws):
ws.send('{"type":"subscribe","symbol":"BINANCE:BTCUSDT"}')
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://ws.finnhub.io?token=****ps2ad3iahju47mdg",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
the output is:
==closed==
2
{'data': [{'c': None, 'p': 20926.99, 's': 'BINANCE:BTCUSDT', 't': 1656017217026, 'v': 0.0008}, {'c': None, 'p': 20927.45, 's': 'BINANCE:BTCUSDT', 't': 1656017217056, 'v': 0.0005}, {'c': None, 'p': 20930.51, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.001}, {'c': None, 'p': 20930.51, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.00061}, {'c': None, 'p': 20930.52, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.1}, {'c': None, 'p': 20931.18, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.05098}, {'c': None, 'p': 20930.5, 's': 'BINANCE:BTCUSDT', 't': 1656017217201, 'v': 0.03}, {'c': None, 'p': 20930.5, 's': 'BINANCE:BTCUSDT', 't': 1656017217201, 'v': 0.03654}, {'c': None, 'p': 20927.9, 's': 'BINANCE:BTCUSDT', 't': 1656017217649, 'v': 0.03}, {'c': None, 'p': 20927.9, 's': 'BINANCE:BTCUSDT', 't': 1656017217649, 'v': 0.01882}], 'type': 'trade'}
only from the first message..?
The goal is to calculate the volume-weighted average price of trades made during a 1 min interval
output should look like this:
2022-05-04 12:22:33 price:20926.99 volume:0.0008
2022-05-04 12:23:43 price:20926.99 volume:0.0058
2022-05-04 12:24:53 price:20927.99 volume:0.0608
I am accessing an API through a get request from which I get a response.
This is my code:
import requests
import json
symbol = "AAPL"
end_point="https://api.polygon.io/v2/snapshot/locale/us/markets/stocks/tickers/"+symbol+"?apiKey=my_key"
r=requests.get(end_point).json()
print(r)
print(r['min'])
print(r) returned:
{'request_id': '2735c0be51de7719fd99460fe8696080', 'status': 'OK', 'ticker': {'day': {'c': 172.93, 'h': 175.48, 'l': 172.37, 'o': 174.14, 'v': 65575561, 'vw': 174.0984}, 'lastQuote': {'P': 172.83, 'S': 3, 'p': 172.82, 's': 1, 't': 1644524332922450142}, 'lastTrade': {'c': None, 'i': '139592', 'p': 172.8199, 's': 2014, 't': 1644524331573573011, 'x': 4}, 'min': {'av': 65559987, 'c': 172.9, 'h': 173.14, 'l': 172.89, 'o': 173.09, 'v': 107429, 'vw': 173.0138}, 'prevDay': {'c': 176.28, 'h': 176.65, 'l': 174.9, 'o': 176.05, 'v': 71204538, 'vw': 175.8287}, 'ticker': 'AAPL', 'todaysChange': -3.46, 'todaysChangePerc': -1.963, 'updated': 1644524331573573011}}
but when I try to access the key "min", I get key error:
KeyError: 'min'
This is super-simple. What am I doing wrong?
Please try:
r['ticker']['min']
the key min is inside ticker. Do this:
r['ticker']['min']
Here we go again. One 'fairly easy' exercise that has finally given me headache.
Make a program that uses a lookup table to convert any set of alphabets into their corresponding NATO phonetic alphabets (https://en.wikipedia.org/wiki/NATO_phonetic_alphabet). Also implement the inverse function.
Input: cat
Output: charlie alfa tango
Inverse function:
Input : charlie alfa tango
Output : cat
I have defined a dictionary with the alphabet and its Nato equivalent for each letter. Then, I have defined a function that will take elements from a list as input. Letter corresponds to my keys in the dictionary and letter conversion corresponds to my values. The function works in the following cases:
When I enter a single character, it will give me its 'Nato' equivalent. (comparing the string with letter and outputting the letter conversion)
When I enter a string that is Nato alphabet it will output the letter. (comparing the string with the letter conversion and outputting the letter)
I'm getting stuck when I need to evaluate the lenght of the string from my list, and then
convert each character into its Nato alphabet.
nato_alphabet = {'a':'Alfa', 'A': 'Alfa', 'b': 'Bravo', 'B': 'Bravo', 'c': 'Charlie', 'C': 'Charlie', 'd': 'Delta', 'D':'Delta', 'e': 'Echo', 'E':'Echo', 'f': 'Foxtrot', 'F': 'Foxtrot', 'G': 'Golf', 'g': 'Golf', 'h': 'Hotel', 'H': 'Hotel', 'k': 'Kilo', 'K': 'Kilo', 'l': 'Lima', 'L': 'Lima', 'm': 'Mike', 'M': 'Mike', 'n': 'November', 'N': 'November', 'o': 'Oscar', 'O': 'Oscar',
'p': 'Papa', 'P': 'Papa', 'q': 'Quebec', 'Q': 'Quebec', 'r': 'Romeo', 'R': 'Romeo', 's': 'Sierra', 'S': 'Sierra', 't':'Tango', 'T': 'Tango', 'u': 'Uniform', 'U': 'Uniform', 'V': 'Victor', 'v': 'Victor', 'w': 'Whiskey', 'W': 'Whiskey', 'y': 'Yankee', 'Y': 'Yankee', 'Z': 'Zulu', 'z': 'Zulu'}
#Passing through a list
def convert(word_to_convert):
word_converted= []
for i in word_to_convert:
for letter, letter_conversion in nato_alphabet.items():
if i == letter:
word_converted.append(letter_conversion)
if i == letter_conversion:
word_converted.append(letter)
if len(i) == letter:
word_converted.append(letter_conversion )
return word_converted
print(convert(['c', 'Alfa', 'moon']))
Thank you everyone for your help !
Diana
Use:
def convert(word_to_convert):
word_converted = []
for i in word_to_convert:
word_converted.append(" ".join(nato_alphabet[ii.lower()] for ii in i))
return word_converted
print(convert(['c', 'Alfa', 'moon']))
Output
['Charlie', 'Alfa Lima Foxtrot Alfa', 'Mike Oscar Oscar November']
As an alternative:
# Passing through a list
def convert(words_to_convert):
word_converted = []
# iterate over each word
for word in words_to_convert:
nato_encoded_list = []
# iterate over each character in word
for char in word:
# fetch the corresponding value from nato_alphabet
nato_encoded_list.append(nato_alphabet[char.lower()])
# join the encoded words and add to word_converted list
word_converted.append(" ".join(nato_encoded_list))
return word_converted
Basically iterate over each, word and for each character in the word fetch the nato_alphabet value.
This might let me know if something is unclear
nato_alphabet = {'a':'Alfa', 'A': 'Alfa', 'b': 'Bravo', 'B': 'Bravo', 'c': 'Charlie', 'C': 'Charlie', 'd': 'Delta', 'D':'Delta', 'e': 'Echo', 'E':'Echo', 'f': 'Foxtrot', 'F': 'Foxtrot', 'G': 'Golf', 'g': 'Golf', 'h': 'Hotel', 'H': 'Hotel', 'k': 'Kilo', 'K': 'Kilo', 'l': 'Lima', 'L': 'Lima', 'm': 'Mike', 'M': 'Mike', 'n': 'November', 'N': 'November', 'o': 'Oscar', 'O': 'Oscar',
'p': 'Papa', 'P': 'Papa', 'q': 'Quebec', 'Q': 'Quebec', 'r': 'Romeo', 'R': 'Romeo', 's': 'Sierra', 'S': 'Sierra', 't':'Tango', 'T': 'Tango', 'u': 'Uniform', 'U': 'Uniform', 'V': 'Victor', 'v': 'Victor', 'w': 'Whiskey', 'W': 'Whiskey', 'y': 'Yankee', 'Y': 'Yankee', 'Z': 'Zulu', 'z': 'Zulu'}
nato_alphabet_inverse = {v.lower():k.lower() for k,v in nato_alphabet.items()}
nato_alphabet = {k.lower():v.lower() for k,v in nato_alphabet.items()}
# print(nato_alphabet_inverse)
def convert(list_to_convert):
return_list = []
for each_item in list_to_convert:
# print(each_item)
if " " in each_item: # Sentence thus must be converted to Alphabets
l_ = each_item.split()
temp_list = []
for each_word in l_:
# print(each_word)
temp_list.append(nato_alphabet_inverse[each_word.lower()])
# return_list.append()
return_list.append("".join(temp_list))
else: # Convert to equivalent Nato
temp_list = []
for chr in each_item:
# print(chr)
temp_list.append(nato_alphabet[chr.lower()])
str_ = " ".join(temp_list)
return_list.append(str_)
return return_list
print(convert(['cAT',"charlie alfa tango"]))
I think I am receiving a dict inside of a list inside of a dict from an API. How do I convert this to a pandas dataframe?
When I use pandas.dataframe.from_dict() the output is just one of the dictionaries in each of the elements, instead of splitting up the dictionaries.
a= {'count': 10465,
'status': 'DELAYED',
'tickers': [{'day': {'c': 48.11,
'h': 49.61,
'l': 48.11,
'o': 48.36,
'v': 2018543,
'vw': 48.6329},
'lastQuote': {'P': 48.07,
'S': 50,
'p': 48.02,
's': 3,
't': 1619195328041448704},
'lastTrade': {'c': None,
'i': '71683813093722',
'p': 48.04,
's': 100,
't': 1619195306417034752,
'x': 4},
'min': {'av': 2018540,
'c': 48.11,
'h': 48.19,
'l': 48.11,
'o': 48.19,
'v': 8842,
'vw': 48.1468},
'prevDay': {'c': 47.83,
'h': 49.54,
'l': 47.07,
'o': 47.9,
'v': 4938809,
'vw': 48.3369},
'ticker': 'FTCH',
'todaysChange': 0.21,
'todaysChangePerc': 0.439,
'updated': 1619195306417034752},
{'day': {'c': 4.01,
'h': 4.09,
'l': 4.01,
'o': 4.03,
'v': 12077,
'vw': 4.0342},
'lastQuote': {'P': 4.09, 'S': 3, 'p': 4, 's': 7, 't': 1619195239632140363},
'lastTrade': {'c': [14, 41],
'i': '4',
'p': 4.01,
's': 100,
't': 1619195239632141482,
'x': 21},
'min': {'av': 12077,
'c': 4.01,
'h': 4.01,
'l': 4.01,
'o': 4.01,
'v': 100,
'vw': 4.01},
'prevDay': {'c': 4.03,
'h': 4.07,
'l': 4.01,
'o': 4.05,
'v': 39628,
'vw': 4.038},
'ticker': 'METC',
'todaysChange': -0.02,
'todaysChangePerc': -0.496,
'updated': 1619195280000000000}
]}
Expected result:
Ticker day_c min_c
FTCH 48.11 48.11
METC 4.01 4.01
If data is your dictionary from the question:
df = pd.DataFrame(
[
{
"Ticker": t["ticker"],
"day_c": t["day"]["c"],
"min_c": t["min"]["c"],
}
for t in data["tickers"]
]
)
print(df)
Prints:
Ticker day_c min_c
0 FTCH 48.11 48.11
1 METC 4.01 4.01
Using json_normalize with df.join and df.pop
For earlier versions:
out = pd.DataFrame(a)
out = out.join(pd.io.json.json_normalize(out.pop('tickers')))
For latest versions:
out = pd.DataFrame(a)
out = out.join(pd.json_normalize(out.pop('tickers')))
Output:
count status ticker todaysChange todaysChangePerc updated \
0 10465 DELAYED FTCH 0.21 0.439 1619195306417034752
1 10465 DELAYED METC -0.02 -0.496 1619195280000000000
day.c day.h day.l day.o ... min.l min.o min.v min.vw prevDay.c \
0 48.11 49.61 48.11 48.36 ... 48.11 48.19 8842 48.1468 47.83
1 4.01 4.09 4.01 4.03 ... 4.01 4.01 100 4.0100 4.03
prevDay.h prevDay.l prevDay.o prevDay.v prevDay.vw
0 49.54 47.07 47.90 4938809 48.3369
1 4.07 4.01 4.05 39628 4.0380
A bit of an overkill, but you could use jmespath; it comes in handy as your nested data becomes more nested.
Basic Use: if accessing a key, use ., if traversing an array/list, use [] :
extract = jmespath.search("""{ticker: tickers[].ticker,
day_c: tickers[].day.c,
min_c: tickers[].min.c}
""", a)
extract
{'ticker': ['FTCH', 'METC'], 'day_c': [48.11, 4.01], 'min_c': [48.11, 4.01]}
pd.DataFrame(extract)
ticker day_c min_c
0 FTCH 48.11 48.11
1 METC 4.01 4.01
It is pretty fast as well, although nothing beats use of native/raw dictionaries
This question already has an answer here:
Why does the print function return None?
(1 answer)
Closed 2 years ago.
I am all new to programming and have to develop a simple script that converts a word into NATO-language. I have figured out (through Google) how to convert the word to NATO, but it also returns 'None'? Why does it do that?
def textToNato(plainText):
d = {
'A': 'Alpha', 'B': 'Bravo', 'C': 'Charlie',
'D': 'Delta', 'E': 'Echo', 'F': 'Foxtrot',
'G': 'Golf', 'H': 'Hotel', 'I': 'India',
'J': 'Juliett','K': 'Kilo', 'L': 'Lima',
'M': 'Mike', 'N': 'November','O': 'Oscar',
'P': 'Papa', 'Q': 'Quebec', 'R': 'Romeo',
'S': 'Sierra', 'T': 'Tango', 'U': 'Uniform',
'V': 'Victor', 'W': 'Whiskey', 'X': 'X-ray',
'Y': 'Yankee', 'Z': 'Zulu'}
natoText = print('-'.join([d[x] for x in [*plainText.upper()]]))
return natoText
natoText = print(...)
Print returns None, that's the reason behind the issue. What you should do is
natoText = '-'.join([d[x] for x in plainText.upper()])
print (natoText) #if you need to print here
return natoText