为什么会出现解析错误?(Python)
我收到一个解析错误>>解析错误:格式不正确(无效令牌):第1行第1列。为什么会出现解析错误?(Python),python,json,python-2.7,parse-error,Python,Json,Python 2.7,Parse Error,我收到一个解析错误>>解析错误:格式不正确(无效令牌):第1行第1列。 我需要将JSON文件解析为KML文件 请注意,这不是我的全部代码 如果有人能在我做错事情的地方帮助我? 在以下代码中,当我将%(timestampMs)之后的“s”更改为“r”时,我还收到一个错误(ValueError:time data“'2013-07-26T22:34:56Z”与格式“%Y-%m-%dT%H:%m:%SZ”)不匹配: "<TimeStamp><when>%(timestampMs
我需要将JSON文件解析为KML文件
请注意,这不是我的全部代码 如果有人能在我做错事情的地方帮助我?
在以下代码中,当我将
%(timestampMs)
之后的“s”更改为“r”时,我还收到一个错误(ValueError:time data“'2013-07-26T22:34:56Z”与格式“%Y-%m-%dT%H:%m:%SZ”)不匹配:
"<TimeStamp><when>%(timestampMs)s</when></TimeStamp>"
这是回溯:
Traceback (most recent call last):
File "..\mapPlot.py", line 106, in <module>
DrawMapData(GetKmlFiles(),ImageData[0], "Location History.png",
ImageData[1], ImageData[2], ImageData[3],
ImageData[4],ImageData[5],ImageData[6])
File "..\mapPlot.py", line 21, in GetKmlFiles
KmlData.append(ReadKmlFile(dirname, filename))
File "..\mapPlot.py", line 38, in ReadKmlFile
latlist.append(float(ET.fromstring(line)[0].text.split(',')[0]))
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1300, in XML
parser.feed(text)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1642, in feed
self._raiseerror(v)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1506, in _raiseerror
raise err
回溯(最近一次呼叫最后一次):
文件“.\mapPlot.py”,第106行,在
DrawMapData(GetKmlFiles(),ImageData[0],“Location History.png”,
ImageData[1],ImageData[2],ImageData[3],
ImageData[4]、ImageData[5]、ImageData[6])
文件“.\mapPlot.py”,第21行,在GetKmlFiles中
append(ReadKmlFile(dirname,filename))
ReadKmlFile中第38行的文件“.\mapPlot.py”
latlist.append(float(ET.fromstring(line)[0].text.split(',')[0]))
文件“C:\Python27\lib\xml\etree\ElementTree.py”,第1300行,xml格式
parser.feed(文本)
文件“C:\Python27\lib\xml\etree\ElementTree.py”,第1642行,在提要中
自我提升错误(v)
文件“C:\Python27\lib\xml\etree\ElementTree.py”,第1506行,在_raiseerror中
提出错误
以下是我将JSON解析为KML的代码:
import json
import os, time, math
from datetime import datetime
from time import mktime
import xml.etree.ElementTree as ET
from PIL import Image, ImageDraw
def parse_json_data_to_kml_file():
kml_file = open('location.kml', "w")
json_file = open('LocationHistory.json')
json_string = json_file.read()
json_data = json.loads(json_string)
locations = json_data["data"]["items"]
placemark = ["<Placemark>",
"<TimeStamp><when>%(timestampMs)s</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:
location['timestampMs'] =
datetime.fromtimestamp(int(location.get("timestampMs"))/1000)
.strftime("%Y-%m-%dT%H:%M:%SZ")
temp = placemark % location
kml_file.write("\n" + temp + "\n")
kml_file.close()
json_file.close()
parse_json_data_to_kml_file()
导入json
导入操作系统、时间、数学
从日期时间导入日期时间
自始至终
将xml.etree.ElementTree作为ET导入
从PIL导入图像,ImageDraw
def将_json_数据_解析为_kml_文件():
kml_文件=打开('location.kml',“w”)
json_file=open('LocationHistory.json')
json_string=json_file.read()
json_data=json.loads(json_字符串)
位置=json_数据[“数据”][“项目”]
placemark=[“”,
“%(时间戳)s”,
"",
"",
“%(准确度)r]”,
"",
“%(经度)r,%
(纬度)r“,
""]
placemark=“\n”。加入(placemark)
对于位置中的位置:
位置['timestampMs']=
datetime.fromtimestamp(int(location.get(“timestamps”))/1000)
.strftime(“%Y-%m-%dT%H:%m:%SZ”)
温度=位置标记%1
kml_文件。写入(“\n”+temp+“\n”)
kml_文件.close()
json_file.close()
将_json_数据_解析为_kml_文件()
仅显示产生错误的代码和无法解析的输入。请现在查看我的问题?我改变了一些东西@timgebOk,现在给我们完整的回溯和您试图解析的json的内容。现在应该可以了!:)@蒂姆盖比,看!我刚把它修好!我的程序运行顺利!
import json
import os, time, math
from datetime import datetime
from time import mktime
import xml.etree.ElementTree as ET
from PIL import Image, ImageDraw
def parse_json_data_to_kml_file():
kml_file = open('location.kml', "w")
json_file = open('LocationHistory.json')
json_string = json_file.read()
json_data = json.loads(json_string)
locations = json_data["data"]["items"]
placemark = ["<Placemark>",
"<TimeStamp><when>%(timestampMs)s</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:
location['timestampMs'] =
datetime.fromtimestamp(int(location.get("timestampMs"))/1000)
.strftime("%Y-%m-%dT%H:%M:%SZ")
temp = placemark % location
kml_file.write("\n" + temp + "\n")
kml_file.close()
json_file.close()
parse_json_data_to_kml_file()