Issue
I need to get data (property names and values) from JSON-formatted files. I can get what I need from the files as a list of strings. I.e., this always works:
f = open("f.txt", "r")
data = json.load(f);
f.close()
get = data["payload"]["blob"]["rawLines"]
so that when I print "get" I see something like this (below) that contains all of the values and property names I need:
get [' name property1 ', ' 98.00000 property2 ', ' 3.00000 property3 ', ' 500.66300 property4 ', ' -50000.9999 property5 ', ' 100.45200 property6 ', ' 59.75258 property7 ', ' 9.66543 property8 ', ' 0.00000 property9 ', ' 100.07655 property10 ', ' 0.00000 property11 ', ' 0.00000 property12 ', ' 0.00000 property13 ', ' 0.00000 property14 ', ' 8.88888 property15 ', ' 1.00000 property16 ', ' 0.00000 property17 ...
However, when I then make a dict via a strip and split:
mydict = dict(line.strip().split(None, 1) for line in get)
some of the property name-values pairs are missing. For example, property16 and its value are always missing.
I can't post the data but am hoping that someone may know of a more robust method for dealing with the strip and split step. Today I have been looking at previous posts (such as this one) but have not been getting far.
The solution from @Tim Roberts has solved the fundamental problem. By using
mydict = dict(reversed(line.split(None, 1)) for line in get)
I get all of the property name–value pairs that were previously missing.
There is one minor issue. The commas persist. So when I turn the dict into a dataframe
mydataframe = pd.DataFrame(mydict.items(), columns=["name", "value"])
I get
name value
0 Methane CH4
1 Thing2word1 Thing2word2 ... 25.07700
2 Thing3word3 Thing3word3 ... 11.33000
...
Is there a way to get rid of the commas so that there are only two columns, the property name and value? Thank you.
Solution
You don't need "strip" if you do the default split. I suspect what you want is to reverse the key and value, like this:
mydict = dict(reversed(line.split()) for line in get)
Output:
{'property1': 'name', 'property2': '98.00000', 'property3': '3.00000', 'property4': '500.66300', 'property5': '-50000.9999', 'property6': '100.45200', 'property7': '59.75258', 'property8': '9.66543', 'property9': '0.00000', 'property10': '100.07655', 'property11': '0.00000', 'property12': '0.00000', 'property13': '0.00000', 'property14': '0.00000', 'property15': '8.88888', 'property16': '1.00000', 'property17': '0.00000'}
Answered By - Tim Roberts
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.