Python:更改某些JSON数据并写入输出文件 [英] Python: Change certain JSON data and write to output file
问题描述
我正在尝试将.json文件解析为.kml文件,以供绘图程序使用.我将提供一组数据样本以简化此问题:
I am trying to parse a .json file into a .kml file to be used by a plotting program. I am going to give a set a data samples to simplify the issue:
我有一个LocationHistory.json文件,该文件具有以下结构:
I have a LocationHistory.json file that has the 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"
}]
}
}
我定义了一个解析json数据的函数,然后将其馈入地标"字符串以写入(输出)到"location.kml"文件中:
I define a function that parses the json data and then I want to feed it into a "placemark" string to write (output) into a "location.kml" file:
import json
import datetime
def parse_jason_data_to_kml_file():
kml_file = open('location.kml', "r+")
#Here I parse the info inside the 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, I create a 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)
# Now I loop through the json data and write the json data into the
# placemark templete and write each placemark to a KML file.
for location in locations:
temp = placemark % location
kml_file.write("\n" + temp + "\n")
kml_file.close()
json_file.close()
parse_jason_data_to_kml_file()
此代码的目的是将包含.json数据的地标字符串从如下所示的地标字符串写入location.ml文件:
The aim of this code is to write the placemark string that contains the .json data, into the location.ml file, from the placemark string that looks like this:
<Placemark>
<TimeStamp><when>the "timestampMS" value from the JSON data item</when></TimeStamp>
<ExtendedData>
<Data name="accuracy">
<value> the "accuracy" value from the JSON data item </value>
</Data>
</ExtendedData><Point><coordinates>"longitude,latitude"</coordinates></Point>
</Placemark>
对于输出,应如下所示:
To an output, which should look like 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>
一切正常,但是我的问题出在哪里,我需要从例如更改所有"timestampMs"信息(如LocationHistory.json文件中所示). 1374870896803至2013-07-26T10:24:24Z. 通过此代码,我可以将timestampMs作为日期时间对象打印出来:
All is working well as it should but my problem comes in where I need to change all the "timestampMs" info (as seen in LocationHistory.json file) from eg. 1374870896803 to eg. 2013-07-26T10:24:24Z. With this code I can print out the timestampMs as 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: I want to change each timestamp to look like this: (Notice the datetime on the top as the 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>
谢谢
推荐答案
在for循环的开头添加一个步骤:
Add a step to the beginning of the for loop:
location['timestampMs'] = datetime.datetime.fromtimestamp(int(location.get("timestampMs"))/1000).strftime("%Y-%m-%dT%H:%M:%SZ")
这篇关于Python:更改某些JSON数据并写入输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!