python - Stripping timestamp from Json int -
in third-party script, it's meant spit out json me decode instead prepends timestamp:
2015-03-28t16:32:41.875199+00:00 {"1": {"power (kw)": "0.301", "energy imported (kwh)": "62.281"...}}
all of in 1 big integer. i've tried split based on space/whitespace contained before first curly bracket can not it. i'd appreciate pointers - i'm aware questions here basic, , apparently i'm going blocked unless improve, please don't shoot me asking simple one!
updated 31/3/2015 @alex.
i realise may seem impossible assure returns datatype of int. may not believe it, you'll have to, , check guy's code here. i've used call script:
get_power=os.system("python /fetch_neurio.py --ip 172.16.0.8 --format json --type sensor") thedata = get_power print type(thedata)
if find unbelievable @ code of fetch_neurio yourself. if you're struggling cretin me, no worries. came here ask help.
thanks
no idea mean "one big int
" in text and subject since pretty data string, not integer. i'm going answer on assumption repeated use of int
weird repeated typo , meant "string" instead.
given
data = '2015-03-28t16:32:41.875199+00:00 {"1": {"power (kw)": "0.301", "energy imported (kwh)": "62.281"}}'
(removing ...
make json part syntactically valid),
import json timestamp, jsondata = data.split(none, 1) pythondata = json.loads(jsondata)
...yep, is simple this!-)
"splitting on none
" means split on sequences of 1+ whitespace characters (in case blank-looking thing might tab or whatever, don't care:-). second optional argument, here 1
, split method, means 1 split (on first sequence of whitespace) -- producing 2 pieces assign correspondingly 2 variables.
there of course other possibilities, example using regular expressions, if anomalies in data
worse have communicated far (i.e, more pre-pended, whitespace separated timestamp), based on have communicated, simplistic approach should suffice.
Comments
Post a Comment