我正在尝试将.json文件解析为.kml文件以供绘图程序使用。我将给出一组数据样本以简化问题:
我有一个LocationHistory.json文件,它具有以下结构:
{
"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数据的函数,然后我想将它提供给“placemark”字符串以写入(输出)到“location.kml”文件中。
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"]
# Here I get the placemark string structure
placemark = ["<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>"]
placemark = "\n".join(placemark)
# Now i try to find certain substrings in the "placemark" string that I would like to replace:
find_timestamp = placemark.find("the “timestampMS” value from the JSON data item")
find_accuracy = placemark.find("the “accuracy” value from the JSON data item")
find_longitude = placemark.find("”longitude")
find_latitude = placemark.find("latitude”")
# Next, I want to loop through the LocationHistory.json file (the data list above)
# and replace the strings with the parsed LocationHistory.json data saved as:
# location["timestampMs"], location["accuracy"], location["longitude"], location["latitude"]:
for location in locations:
if find_timestamp != -1:
placemark.replace('the “timestampMS” value from the JSON data item', location["timestampMs"])
if find_accuracy != -1:
placemark.replace('the “accuracy” value from the JSON data item', location["accuracy"])
if find_longitude != -1:
placemark.replace('”longitude', location["longitude"])
if find_latitude != -1:
placemark.replace('latitude”', location["latitude"])
kml_file.write(placemark)
kml_file.write("\n")
kml_file.close()
此代码的目的是将包含.json数据的地标字符串写入location.ml文件,该地标字符串如下所示:
<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>
输出,如下所示:
<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>
<Placemark>
<TimeStamp><when>2013-07-26T22:31:51Z</when></TimeStamp>
<ExtendedData>
<Data name=”accuracy”>
<value>2016</value>
</Data>
</ExtendedData><Point><coordinates>-85.3526902,34.9857898</coordinates></Point>
</Placemark>
如果我尝试运行此代码,则会收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/Elysium/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/Users/Elysium/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
builtins.execfile(filename, *where)
File "/Users/Elysium/Dropbox/LeeV/Task 5 - Location Plotting/ParseToKML.py", line 77, in <module>
parse_jason_data_to_kml_file()
File "/Users/Elysium/Dropbox/LeeV/Task 5 - Location Plotting/ParseToKML.py", line 51, in parse_jason_data_to_kml_file
placemark.replace('the “timestampMS” value from the JSON data item', location["timestampMs"])
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 33: ordinal not in range(128)
这是我的代码或软件问题吗? 从各种来源我可以发现它可能与PIP或ascii或其他东西有关,但没有答案可以帮助我......我确定我已经安装了pip ......
任何帮助将不胜感激。我们也欢迎任何改进我的代码的建议:)
谢谢
答案 0 :(得分:1)
您可以按以下方式尝试:
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)
for location in locations:
temp = placemark % location
kml_file.write(temp)
kml_file.write("\n")
这里,在%(any_dict_key)r
中,r转换任何Python对象(这里是any_dict_key的值),然后将其插入到字符串中。因此,对于您的时间戳,您必须将其转换为datetime
对象。
您可以阅读文档的这一部分以查看详细信息 - string-formatting-operations