为什么我会得到Parse错误? (蟒蛇)

时间:2017-04-06 11:25:55

标签: python json python-2.7 parse-error

我收到一个Parse错误>> ParseError:格式不正确(令牌无效):第1行第1列 我需要将JSON文件解析为KML文件!
请注意,这不是我的全部代码!

如果有人能帮助我,那我做错了什么? 我也收到错误(ValueError:时间数据“'2013-07-26T22:34:56Z'”与格式'%Y-%m-%dT%H:%M:%SZ'不匹配)当我是在此代码中%(timestampMs)之后将“s”更改为“r”:

"<TimeStamp><when>%(timestampMs)s</when></TimeStamp>"  

这是JSON文件的内容(只是几个)忽略“kind”类型:

{
  "data" :{
    "items" :[{
      "kind" : "latitude#location",
      "timestampMs" : "1374870896803",
      "latitude" : 34.9482949,
      "longitude" : -85.3245474,
      "accuracy" : 2149
    }]
  }
}

这是追溯:

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

这是我将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()

0 个答案:

没有答案