将 JSON 文件解析为 KML 文件 ascii 错误
Parse a JSON file into a KML file ascii error
我正在尝试将 .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...
如有任何帮助,我们将不胜感激。也欢迎任何改进我的代码的建议:)
谢谢
您可以尝试如下-
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
我正在尝试将 .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...
如有任何帮助,我们将不胜感激。也欢迎任何改进我的代码的建议:)
谢谢
您可以尝试如下-
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