Python: Change certain JSON data and write to output file -


i trying parse .json file .kml file used plotting program. going give set data samples simplify issue:

i have locationhistory.json file has following structure:

{  "data" : {    "items" : [ {      "kind" : "latitude#location",      "timestampms" : "1374870896803",      "latitude" : 34.9482949,      "longitude" : -85.3245474,      "accuracy" : 2149    }, {      "kind" : "latitude#location",      "timestampms" : "1374870711762",      "latitude" : 34.9857898,      "longitude" : -85.3526902,      "accuracy" : 2016"    }]   } } 

i define function parses json data , want feed "placemark" string write (output) "location.kml" file:

import json import datetime  def parse_jason_data_to_kml_file():    kml_file = open('location.kml', "r+")     #here parse info inside locationhistory.json file    json_file = open('locationhistory.json')    json_string = json_file.read()    json_data = json.loads(json_string)     locations = json_data["data"]["items"]     # next, create placemark string template:    placemark = ["<placemark>",                 "<timestamp><when>%(timestampms)r</when></timestamp>",                 "<extendeddata>",                 "<data name=\"accuracy\">",                 "<value>%(accuracy)r</value>]",                 "</data>",                 "</extendeddata><point><coordinates>%(longitude)r, %(latitude)r</coordinates></point>",                 "</placemark>"]    placemark = "\n".join(placemark)     # loop through json data , write json data    # placemark templete , write each placemark kml file.    location in locations:        temp = placemark % location        kml_file.write("\n" + temp + "\n")     kml_file.close()    json_file.close()     parse_jason_data_to_kml_file() 

the aim of code write placemark string contains .json data, location.ml file, placemark string looks this:

<placemark> <timestamp><when>the “timestampms” value json data item</when></timestamp> <extendeddata> <data name=”accuracy”> <value> “accuracy” value json data item </value> </data> </extendeddata><point><coordinates>”longitude,latitude”</coordinates></point> </placemark> 

to output, should this:

<placemark> <timestamp><when>u'1374870896803'</when></timestamp> <extendeddata> <data name=”accuracy”> <value>2149</value> </data> </extendeddata><point><coordinates>-85.3245474,34.9482949</coordinates></point> </placemark>  <placemark> <timestamp><when>u'1374870711762'</when></timestamp> <extendeddata> <data name=”accuracy”> <value>2016</value> </data> </extendeddata><point><coordinates>-85.3526902,34.9857898</coordinates></point> </placemark> 

all working should problem comes in need change "timestampms" info (as seen in locationhistory.json file) eg. 1374870896803 eg. 2013-07-26t10:24:24z. code can print out timestampms datetime objects:

for location in locations:    print datetime.datetime.fromtimestamp(int(location.get("timestampms"))/1000).strftime("%y-%m-%dt%h:%m:%sz") 

so in other words: want change each timestamp this: (notice datetime on top timestamp)

<placemark> <timestamp><when>2013-07-26t22:34:56z</when></timestamp> <extendeddata> <data name=”accuracy”> <value>2149</value> </data> </extendeddata><point><coordinates>-85.3245474,34.9482949</coordinates></point> </placemark> 

thank you

add step beginning of loop:

location['timestampms'] = datetime.datetime.fromtimestamp(int(location.get("timestampms"))/1000).strftime("%y-%m-%dt%h:%m:%sz") 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -