无法在Python中读取JSON文件 [英] Can't read JSON file in Python

查看:413
本文介绍了无法在Python中读取JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从JSON文件中读取数据,但是遇到奇怪的错误,不知道这意味着什么.我尝试使用Google搜索,但没有帮助.我收到以下错误:

I tried to read data from a JSON file, but I encountered weird error and have no idea what it means. I tried googling it, but it didn't help. I got the following error:

Traceback (most recent call last):
  File "items_uploader.py", line 40, in <module>
    main()
  File "items_uploader.py", line 16, in main
    LoadItemsData(settings['items_filename'])
  File "items_uploader.py", line 36, in LoadItemsData
    data = json.load(json_data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 278, in load
    **kw)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 8 column 397 (char 3064)

代码本身非常简单:

import socket
import MySQLdb
from ConfigParser import SafeConfigParser
import json
from pprint import pprint

def main():
    settings = GetSettings()
    LoadItemsData(settings['items_filename'])
    return

def GetSettings():
    settings = {}
    parser = SafeConfigParser()
    parser.read('settings.yaml')
    settings['items_filename'] =  parser.get('files', 'items_filename')
    return settings

def LoadItemsData(filename):
    json_data=open(filename)
    data = json.load(json_data)
    return data

if __name__ == '__main__':
    main()

任何帮助将不胜感激!

推荐答案

请确保您的JSON数据采用有效格式,另外一个字符将会弄乱python解析器.要测试您的JSON数据,请此处,请确保您可以正确的格式查看它.

Make sure your JSON data is in a valid format, one extra character will mess up the python parser. To test your JSON data go here, make sure you can see it in a correct format.

例如,如果我有

JSON_data ='{"c":[{"xy":{"xstart":0,"xend":5,"ystart":1,"yend":5},"names":["D","T","O","H","L","C",],"co":["rgb(0,0,128)"]}],"Values":{"D":["11/30/2012"],"T":["09:44:00"],"O":["5848.40"],"H":["5848.40"],"L":["5847.45"],"C":["5848.40"]}}'

C之后的,(此处为["D","T","O","H","L","C",])将显示错误.因此,请确保您的数据格式正确,并且没有不必要的字符.

The , after C (here ["D","T","O","H","L","C",]) will show an error. So make sure that your data is in correct format and there are no unnecessary characters.

希望这会有所帮助.

这篇关于无法在Python中读取JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆