-3

I have a JSON data set that looks like this:

{"sequence":109428985,"bids":[["0.1243","53",5],["0.12429","24",2],["0.12428","6",1],["0.12427","6",2],["0.12426","6",1],["0.12425","6",1],["0.12424","6",1],["0.12423","6",1],["0.12422","6",1],["0.12421","6",1],["0.124206","6496",2],["0.124205","36032",1],["0.124201","20",1],["0.1242","191",2],["0.124193","400",1],["0.12419","6",1],["0.124189","1214",1],["0.12418","6",1],["0.12417","6",1],["0.12416","6",1],["0.12415","6",1],["0.12414","6",1],["0.12413","6",1],["0.12412","6",1],["0.12411","6",1],["0.1241","6",1],["0.12409","6",1],["0.12408","6",1],["0.12407","6",1],["0.12406","6",1],["0.12405","6",1],["0.124044","14",1],["0.12404","6",1],["0.12403","6",1],["0.12402","6",1],["0.12401","6",1],["0.124","64576",5],["0.12399","6",1],["0.12398","6",1],["0.12397","6",1],["0.12396","6",1],["0.12395","6",1],["0.12394","6",1],["0.12393","6",1],["0.12392","6",1],["0.12391","6",1],["0.1239","6",1],["0.12389","6",1],["0.12388","6",1],["0.12387","6",1]],"asks":[["0.124304","20",1],["0.12434","6",1],["0.12435","6",1],["0.12436","6",1],["0.12437","6",2],["0.12438","6",2],["0.12439","7",2],["0.124453","20",1],["0.124481","1",1],["0.124559","535",1],["0.12456","1210",1],["0.124566","10058",1],["0.124601","7480",1],["0.12462","19",2],["0.124621","1",1],["0.12463","6",1],["0.12464","6",1],["0.12465","6",1],["0.12466","6",1],["0.124668","5306",1],["0.124669","1",1],["0.12467","6",1],["0.124671","20",1],["0.124674","691",1],["0.12468","6",1],["0.124683","20",1],["0.12469","6",1],["0.124697","20",1],["0.1247","6",1],["0.12471","6",1],["0.12472","6",1],["0.12473","6",1],["0.12474","6",1],["0.12475","6",1],["0.12476","6",1],["0.12477","7",2],["0.124779","20",1],["0.12478","7",2],["0.124784","12",1],["0.12479","6",1],["0.124796","1",1],["0.1248","6",1],["0.12481","6",1],["0.12483","6",1],["0.12484","6",1],["0.12485","6",1],["0.124855","1",1],["0.12487","6",1],["0.124889","16500",1],["0.12489","6",1]]}

I am trying to pull the data from the first two elements in the lists, and change them so I can put them in as numbers into a database.

For example, this:

["0.1243","53",5],["0.12429","24",2],["0.12428","6",1]

Should change to this:

0.1243 53
0.12429 24
0.12428 6

Here is my current code:

import requests
import json
import re

url = 'https://api.pro.coinbase.com/products/BAT-USDC/book?level=2'

trade_data = requests.get(url).json()

bid_data = trade_data['bids']
print(bid_data)

This is what is returned:

[[u'0.124456', u'1158', 3], [u'0.12445', u'6', 1], [u'0.12442', u'6', 1], [u'0.12441', u'6', 1], [u'0.1244', u'6', 1], [u'0.12439', u'6', 1], [u'0.12438', u'6', 1], [u'0.12437', u'6', 2], [u'0.12436', u'6', 2], [u'0.12435', u'6', 1], [u'0.12434', u'6', 1], [u'0.12433', u'6', 1], [u'0.124315', u'6991', 1], [u'0.124314', u'1212', 1], [u'0.1243', u'6', 3], [u'0.12429', u'6', 2], [u'0.12428', u'6', 1], [u'0.12427', u'6', 2], [u'0.124261', u'2419', 1], [u'0.12426', u'6', 1], [u'0.124251', u'20', 1], [u'0.12425', u'6', 1], [u'0.12424', u'6', 1], [u'0.12423', u'6', 1], [u'0.12422', u'6', 1], [u'0.12421', u'6', 1], [u'0.124206', u'20', 1], [u'0.124205', u'36032', 1], [u'0.124201', u'20', 1], [u'0.1242', u'391', 3], [u'0.124193', u'400', 1], [u'0.12419', u'6', 1], [u'0.12418', u'6', 1], [u'0.12417', u'6', 1], [u'0.12416', u'6', 1], [u'0.12415', u'6', 1], [u'0.12414', u'6', 1], [u'0.12413', u'6', 1], [u'0.12412', u'6', 1], [u'0.12411', u'6', 1], [u'0.1241', u'6', 1], [u'0.12409', u'6', 1], [u'0.12408', u'6', 1], [u'0.12407', u'6', 1], [u'0.12406', u'6', 1], [u'0.12405', u'6', 1], [u'0.124044', u'14', 1], [u'0.12404', u'6', 1], [u'0.12403', u'6', 1], [u'0.12402', u'6', 1]]

I do not know where the u's came from, and I do not know how to isolate the 2 numbers of importance to my calculations from this "list in list" format. I am a programming noob, and I apologize for that.

Any help is greatly appreciated, and thanks in advance!

Closest previous question I have found:

how to extract string inside single quotes using python script

2 Answers2

0

The u prefix simply means that

  • You ran the code in Python 2
  • The strings are in unicode

All you have to do is simply iterate over the list and convert what you want to float:

bids = data["bids"]

for li in bids:
    li[:2] = map(float, li[:2])

print bids
# [[0.1243, 53.0, 5], [0.12429, 24.0, 2], ... ]
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
0

When you are dealing with lists so called list-comprehensions are handy, example:

data = [["0.1243","53",5],["0.12429","24",2],["0.12428","6",1]]
firsttwo = [i[:2] for i in data]
numbers = [[float(i[0]),int(i[1])] for i in firsttwo]
print(numbers) #prints [[0.1243, 53], [0.12429, 24], [0.12428, 6]]

Note that this assumes that first number is always float and second number is integer. i[:2] is usage of slicing, it might be understand as: cut list after 2nd element and get "left" part (i.e. before cut). You can read more about list-comprehensions here: https://www.pythonforbeginners.com/basics/list-comprehensions-in-python

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • Wow, thank you. You totally answered my next few questions. I'll get a joint for you too bro!! Thank you for real, that super helps. – cocoadreamboy Jan 15 '19 at 13:48