Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 加载序列化json对象时出现问题_Python_Json_Python 2.7 - Fatal编程技术网

Python 加载序列化json对象时出现问题

Python 加载序列化json对象时出现问题,python,json,python-2.7,Python,Json,Python 2.7,我使用json.dumps将一些数据写入文件。我现在尝试使用json.load和json.load在另一个工具中读取此数据,并得到以下错误: Traceback (most recent call last): File "./compute_avg_cpu_util.py", line 16, in <module> data = json.loads(line.rstrip()) File "/usr/local/python-2.76/lib/python2.7

我使用json.dumps将一些数据写入文件。我现在尝试使用json.load和json.load在另一个工具中读取此数据,并得到以下错误:

Traceback (most recent call last):
  File "./compute_avg_cpu_util.py", line 16, in <module>
    data = json.loads(line.rstrip())
  File "/usr/local/python-2.76/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/local/python-2.76/lib/python2.7/json/decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/python-2.76/lib/python2.7/json/decoder.py", line 383, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
这是我的代码(现在):

我这样做可能会错误地写入这些数据文件(这些数据来自对存储设备的REST api调用):


1) 尝试捕获任何异常:

  try:
    data = json.loads(line.rstrip())
  except ValueError as ve:
    print "ERROR: {0} cannot be parsed, exception message:{1}".format(line, ve)
    continue
(二) 我还测试了您的json示例,它可以工作:

Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> json.loads('{"sample": 309896986, "data": {"value": 2}, "samples": 319049477, "startTime": "20151213T00:01:47"}')
{u'sample': 309896986, u'data': {u'value': 2}, u'startTime': u'20151213T00:01:47', u'samples': 319049477}
我将您的示例json行保存到
sample.csv
中,并通过以下代码正确解析它: 导入json

with open("sample.csv") as f:
    for line in f:
        data = json.loads(line.rstrip())
        print(data)

我猜你的文件中可能有一些未格式化的行。您需要调试您的行。

您应该按照自己的建议,将f中的行的
更改为
。readlines():
更改为
f中的行:
仍然会收到相同的错误。我将上面的代码片段更新为我现在正在处理的内容--得到了完全相同的错误。不需要将打开(文件)的
更改为f:
,默认为
“r”
(读取)模式。@BenH尝试添加
try except
以捕获异常并查看哪一行无法解析,文件的第一行是否为空?将f.readlines()中的行的
更改为f:
中的行的
,实际上只会影响文件是否首先完全读入内存。在任何一种情况下,每一行都将一次迭代处理一行。真正的问题是,通常无法逐行解析格式正确的JSON文件。
data=JSON.load(open(filename,'rb'))
可能有一行格式不正确-在处理文件名和行号时,您可能希望打印文件名和行号,以查看它实际引发此异常的位置。最好捕获异常,因为它可能会让您知道文件有问题(以及什么/哪里)。您更新的代码只是默默地忽略任何垃圾。
  try:
    data = json.loads(line.rstrip())
  except ValueError as ve:
    print "ERROR: {0} cannot be parsed, exception message:{1}".format(line, ve)
    continue
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> json.loads('{"sample": 309896986, "data": {"value": 2}, "samples": 319049477, "startTime": "20151213T00:01:47"}')
{u'sample': 309896986, u'data': {u'value': 2}, u'startTime': u'20151213T00:01:47', u'samples': 319049477}
with open("sample.csv") as f:
    for line in f:
        data = json.loads(line.rstrip())
        print(data)