I have this string:
{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22",
"low":"265.6", "prevClose":"266.75","close":"265.66"}
{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,
"bidexch":"Q","biddate":"1513293904000","ask":265.42,
"asksz":45,"askexch":"P","askdate":"1513294015000"}
{"type":"summary","symbol":"SPY","open":"267.09",
"high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}
If I do:
type(string)
I get:
<type 'unicode'>
If I do:
type(firstString)
where firstString is just the first of the three parts of the string, I get:
<type 'unicode'>
With Python, how can I split it based on the external parentheses, such that from this one string, we obtain three strings, each one having the form "{ ... }"?
If there are no nested objects and newlines are not guaranteed between parts, as you've said, it's as simple as splitting on }:
your_string = '''{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22",
"low":"265.6", "prevClose":"266.75","close":"265.66"}
{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,
"bidexch":"Q","biddate":"1513293904000","ask":265.42,
"asksz":45,"askexch":"P","askdate":"1513294015000"}
{"type":"summary","symbol":"SPY","open":"267.09",
"high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}'''
substrings = [part.strip() + "}" for part in your_string.split("}") if part.strip()]
# ['{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22",
# "low":"265.6", "prevClose":"266.75","close":"265.66"}',
# '{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,
# "bidexch":"Q","biddate":"1513293904000","ask":265.42,
# "asksz":45,"askexch":"P","askdate":"1513294015000"}',
# '{"type":"summary","symbol":"SPY","open":"267.09",
# "high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}']
Or you can immediately parse the parts into individual Python dictionaries:
dicts = [json.loads(part + "}") for part in your_string.split("}") if part.strip()]
# [{'open': '267.09', 'high': '267.22', 'prevClose': '266.75', 'type': 'summary',
# 'close': '265.66', 'low': '265.6', 'symbol': 'SPY'},
# {'askdate': '1513294015000', 'bid': 265.38, 'asksz': 45, 'type': 'quote',
# 'ask': 265.42, 'bidsz': 3, 'bidexch': 'Q', 'biddate': '1513293904000',
# 'askexch': 'P', 'symbol': 'SPY'},
# {'open': '267.09', 'high': '267.22', 'prevClose': '266.75', 'type': 'summary',
# 'close': '265.66', 'low': '265.6', 'symbol': 'SPY'}]
You can split the string on the newline character and parse it using the json module.
import json
s = '''{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}
{"type":"quote","symbol":"SPY","bid":265.38,"bidsz":3,"bidexch":"Q","biddate":"1513293904000","ask":265.42,"asksz":45,"askexch":"P","askdate":"1513294015000"}
{"type":"summary","symbol":"SPY","open":"267.09","high":"267.22","low":"265.6","prevClose":"266.75","close":"265.66"}'''
[json.loads(line) for line in s.split('\n')]
# returns:
[{'close': '265.66',
'high': '267.22',
'low': '265.6',
'open': '267.09',
'prevClose': '266.75',
'symbol': 'SPY',
'type': 'summary'},
{'ask': 265.42,
'askdate': '1513294015000',
'askexch': 'P',
'asksz': 45,
'bid': 265.38,
'biddate': '1513293904000',
'bidexch': 'Q',
'bidsz': 3,
'symbol': 'SPY',
'type': 'quote'},
{'close': '265.66',
'high': '267.22',
'low': '265.6',
'open': '267.09',
'prevClose': '266.75',
'symbol': 'SPY',
'type': 'summary'}]
Related
Hello I need Output as SILVERM JUN FUT by parsing json response to symbol using Python
follow is the Json Response that I received
{'exchange': 'MCX', 'token': 221599, 'ltp': 71480.0, 'ltt': 1623072465, 'ltq': 1, 'volume': 20227, 'best_bid_price': 71476.0, 'best_bid_quantity': 2, 'best_ask_price': 71487.0, 'best_ask_quantity': 2, 'total_buy_quantity': 2086, 'total_sell_quantity': 2163, 'atp': 71205.04, 'exchange_time_stamp': 1623072466, 'open': 71380.0, 'high': 71550.0, 'low': 70890.0, 'close': 71585.0, 'yearly_high': 71550.0, 'yearly_low': 0.0, 'instrument': Instrument(exchange='MCX', token=221599, symbol='SILVERM JUN FUT', name='', expiry=datetime.date(2021, 6, 30), lot_size=None)}
as Instrument Data is contained in ( ) rounded brackets how can i parse beyond instrument object
update: It was not python dict() as per the person who removed his comment. but a named tuple
following solution worked for me:
P = response['instrument']
print(P.symbol)
I want to convert input json to nested json defined, I am not able to think of any json library which help me achieve this
Input json
[{'Name': 'John', 'state': 'Boston', 'currency': 'USD', 'marks': 100},
{'Name': 'Rohan', 'state': 'Paris', 'currency': 'EUR', 'marks': 20},
{'Name': 'Rohan', 'state': 'Lyon', 'currency': 'EUR', 'marks': 11.4},
{'Name': 'Messi', 'state': 'Madrid', 'currency': 'EUR', 'marks': 9.9},
{'Name': 'Lampard', 'state': 'London', 'currency': 'GBP', 'marks': 12.2},
{'Name': 'Lampard', 'state': 'London', 'currency': 'FBP', 'marks': 10.9}]
output json
{
"USD": {
"John": {
"Boston": [
{
"Marks": 100
}
]
},
Current scenario based on value Currency,Name,state,marks
The nested json can be put upto n level if required such as Name and state and marks or it can be Name , curreny , state and marks or Name,curreny and marks
So you want currency > name > state > list of marks.
One solution would be to create the structure using defaultdicts, and then just add to it.
from collections import defaultdict
from functools import wraps
data = [...]
def ddmaker(type_):
#wraps(dict)
def caller():
return defaultdict(type_)
return caller
# create the structure of the output
output = defaultdict(ddmaker(ddmaker(list)))
# add to it
for item in data:
currency = item["currency"]
name = item["Name"]
state = item["state"]
mark = item["marks"]
output[currency][name][state].append({'Marks': mark})
I'm trying to assign two values of a list to two different variables. Here is the json list. Its raising key error. Please let me know where I am wrong.
[{'min': 1.158, 'max': 1.150, 'id': 269097, 'to': 1532003820, 'from': 1532003760, 'check': 1.15852, 'no_check': 1.15822, 'volume': 0},{'min': 1.1, 'max': 1.17, 'id': 269098, 'to': 1532003880, 'from': 1532003820, 'check': 1.158615, 'nocheck': 1.158515, 'volume': 0}]
Here is my code python3 code:
pt = [{'min': 1.158, 'max': 1.150, 'id': 269097, 'to': 1532003820, 'from': 1532003760, 'check': 1.15852, 'no_check': 1.15822, 'volume': 0},{'min': 1.1, 'max': 1.17, 'id': 269098, 'to': 1532003880, 'from': 1532003820, 'check': 1.158615, 'nocheck': 1.158515, 'volume': 0}]
y = [item[0] for item in pt]
z = [item[0] for item in pt]
print(y)
print(z)
Error:
File "test_YourM.py", line 19, in <module>
y = [item[0][0] for item in pt] File "test_YourM.py", line 19, in <listcomp>
y = [item[0][0] for item in pt] KeyError: 0
Expected output:
print(y) # {'min': 1.158, 'max': 1.150, 'id': 269097, 'to': 1532003820, 'from': 1532003760, 'check': 1.15852, 'no_check': 1.15822, 'volume': 0}
print(z) # {'min': 1.1, 'max': 1.17, 'id': 269098, 'to': 1532003880, 'from': 1532003820, 'check': 1.158615, 'nocheck': 1.158515, 'volume': 0}
[item for item in pt[0]]
[item for item in pt[1]]
The item is generated in in that scope, while pt isn't, even though you're enumerating a dict, you may want to do something like this:
{key: value for key, value in pt[0].items()}
{key: value for key, value in pt[1].items()}
I am new to Python. How can I store the following data as a list inside of another list?
inputList = [[{'timestamp': '2017-10-28T00:00:00.000Z', 'open': '0.051430', 'close': '0.051210', 'min': '0.050583', 'max': '0.051955', 'volume': '30953.184', 'volumeQuote': '1584.562468339'}, {'timestamp': '2017-10-29T00:00:00.000Z', 'open': '0.051191', 'close': '0.049403', 'min': '0.048843', 'max': '0.053978', 'volume': '42699.215', 'volumeQuote': '2190.567660769'}],[{'timestamp': '2017-10-28T00:00:00.000Z', 'open': '0.063390', 'close': '0.072991', 'min': '0.062544', 'max': '0.073524', 'volume': '199636.573', 'volumeQuote': '13427.870355674'}, {'timestamp': '2017-10-29T00:00:00.000Z', 'open': '0.072840', 'close': '0.073781', 'min': '0.069449', 'max': '0.090833', 'volume': '284448.623', 'volumeQuote': '21687.962221794'}]]
Output should be:
outputList = [[0.051210, 0.049403],[0.072991, 0.073781]]
and what I have so far is:
[0.051210, 0.049403, 0.072991, 0.073781]
I use the following code:
insideLoop = []
outputList = []
for list in inputList:
for i, v in enumerate(list):
closing = float(v['close'])
insideLoop.append(closing)
outputList.append(insideLoop)
To be noted that the inputList can be several lists long.
Any solution to this?
Thanks a lot!
You can use a simple list comprehension
result = [[x['close'], y['close']] for x, y in inputList]
print(result)
# - > [['0.051210', '0.049403'], ['0.072991', '0.073781']]
Update
For undetermined number of elements in sublist, use nested list comprehension
result = [[x['close'] for x in y] for y in inputList]
print(result)
# - > [['0.051210', '0.049403'], ['0.072991', '0.073781']]
You can use a nested list comprehension:
s = [[{'timestamp': '2017-10-28T00:00:00.000Z', 'open': '0.051430', 'close': '0.051210', 'min': '0.050583', 'max': '0.051955', 'volume': '30953.184', 'volumeQuote': '1584.562468339'}, {'timestamp': '2017-10-29T00:00:00.000Z', 'open': '0.051191', 'close': '0.049403', 'min': '0.048843', 'max': '0.053978', 'volume': '42699.215', 'volumeQuote': '2190.567660769'}],[{'timestamp': '2017-10-28T00:00:00.000Z', 'open': '0.063390', 'close': '0.072991', 'min': '0.062544', 'max': '0.073524', 'volume': '199636.573', 'volumeQuote': '13427.870355674'}, {'timestamp': '2017-10-29T00:00:00.000Z', 'open': '0.072840', 'close': '0.073781', 'min': '0.069449', 'max': '0.090833', 'volume': '284448.623', 'volumeQuote': '21687.962221794'}]]
new_s = [[float(i['close']) for i in b] for b in s]
Output:
[[0.051210, 0.049403], [0.072991, 0.073781]]
Try this:
result = []
for a in inputList:
res = []
for i in a:
res.append(i['close'])
result.append(res)
print(result)
I have some text data
>>> print content
Date,Open,High,Low,Close,Volume,Adj Close
2015-03-17,4355.83,4384.98,4349.69,4375.63,1724370000,4375.63
2015-03-16,4338.29,4371.46,4327.27,4370.47,1713480000,4370.47
2015-03-13,4328.09,4347.87,4289.30,4314.90,1851410000,4314.90
2015-03-12,4302.73,4339.20,4300.87,4336.23,1855110000,4336.23
2015-03-11,4336.05,4342.87,4304.28,4305.38,1846020000,4305.38
Now I want to convert this into a Dict, so that I can load this into a database using the cursor.executemany that allows me to provide dict as an input.
Is there a module to auto convert this into a Dict. I looked at Numpy - loadtext but that requires me to write this first to a file. Is there a way that i can do this without creating a file?
Use csv.DictReader
>>> with open('text.txt') as f:
... dreader = csv.DictReader(f)
... for row in dreader:
... print(row)
...
{'Adj Close': '4375.63', 'High': '4384.98', 'Volume': '1724370000', 'Low': '4349.69', 'Close': '4375.63', 'Open': '4355.83', 'Date': '2015-03-17'}
{'Adj Close': '4370.47', 'High': '4371.46', 'Volume': '1713480000', 'Low': '4327.27', 'Close': '4370.47', 'Open': '4338.29', 'Date': '2015-03-16'}
{'Adj Close': '4314.90', 'High': '4347.87', 'Volume': '1851410000', 'Low': '4289.30', 'Close': '4314.90', 'Open': '4328.09', 'Date': '2015-03-13'}
{'Adj Close': '4336.23', 'High': '4339.20', 'Volume': '1855110000', 'Low': '4300.87', 'Close': '4336.23', 'Open': '4302.73', 'Date': '2015-03-12'}
{'Adj Close': '4305.38', 'High': '4342.87', 'Volume': '1846020000', 'Low': '4304.28', 'Close': '4305.38', 'Open': '4336.05', 'Date': '2015-03-11'}
I looked at Numpy - loadtext but that requires me to write this first to a file. Is there a way that I can do this without creating a file?
You can use a file like object if you do not want to have physical data.
Use tempfile.TemporaryFile
from tempfile import TemporaryFile
with TemporaryFile('w+t') as flike:
flike.write(content)
flike.seek(0)
dreader = csv.DictReader(flike)
for row in dreader:
#do something
Use io.StringIO
import io #python3 or import StringIO in python2
flike = io.StringIO(content)
for row in csv.DictReader(flike)
#do something