Showing posts with label dictionary. Show all posts
Showing posts with label dictionary. Show all posts

Wednesday, July 31, 2013

Reading from a JSON Dictionary



Dictionaries are useful data structures, and at their core, are a set of one-to-one mappings of data.

A loop and append pattern should work fine for getting the data into the dictionary.

(Of course using the JSON library would greatly simplify this process.)


from urllib.request import urlopen

html = urlopen("http://www.leafly.com/api/strains")

theDataArray = str(html.read()).split('},{')

dictionary = {}

for i in range(100):

      dictionary[theDataArray[i].split(',')[1].split('"')[3]] = theDataArray[i].split(',')[2].split('"')[3]

That last line might look like a lot, but it's mostly just parsing away all the commas and other characters to get at the two pieces of information we're going for, key and name.

This will also take in only 100 strains, for all change to 100 to len(theDataArray), just remember to properly parse the data to avoid errors.

Parsing in Python





Let's make sense of this pile of data.

Reading and parsing data are the first two building blocks of a larger process, compiling, so it's always useful to know a couple of good methods.

A quick Google search of "python reading data" got me to http://docs.python.org/2/tutorial/inputoutput.html, for some simple reference material.

Now, testing is always a fun way to find out what's going on behind the code, so let's see if we can just pull out a bit of the important data here so we don't overwhelm ourselves.

Now, continuing from before, lets convert the data object to a useful string/array.

>> theData = str(html.read()).split(',')

Now let's see what this looks like.

>> for i in range(5):


         print(i, theData[i])


This smaller data structure will come in handy when setting up our larger program design.

Here's the result:

0 b'{"Key":"100-og"

1 "Name":"$100 OG"

2 "Category":"Hybrid"

3 "Symbol":"100"

4 "Abstract":"$100 OG is a hybrid cannabis strain that originally became popular in Southern California. It produces large flowers and a potent high."


Now, wait, your probably thinking, what's that b'{ doing there?

This data is meant to be parsed with the use of json modules, not bare bones python split statements, and that should massively simplify the process.


Although; you may have to redesign your parsing algorithm to completely remove all persisting characters and strings.