Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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_Python 3.x - Fatal编程技术网

使用Python解析JSON的行

使用Python解析JSON的行,python,python-3.x,Python,Python 3.x,我有这个方法: lines = rec.split("\n") rec = '' size = len(lines) i=0 for line in lines: try: self.on_data(json.load(line)) except: logging.warning ('warning, could not parse line:', line) if i == size - 1: # if i

我有这个方法:

lines = rec.split("\n")
rec = ''
size = len(lines)
i=0
for line in lines:
    try:
        self.on_data(json.load(line))
    except:
        logging.warning ('warning, could not parse line:', line)
        if i == size - 1:
            # if it is the last element, we can keep it, since it might not be complete
            rec+=line
    finally:
        i += 1
我得到这个错误:

Message: 'warning, could not parse line:'
Arguments: ('{"readersCount":0,"uuid":"17f5fe87-5140-4f34-ac32-d325beb6b2a1","key":"bar","lockRequestCount":0,"type":"lock","acquired":true}',)

看起来我需要读取元组的第一个元素还是什么?JSON看起来还可以吗?

如注释中所述,您需要
self.on_数据(JSON.loads(line))


json.loads(line)
是您正在寻找的
self.on_data(json.loads(line))
oh wowza,不会发现这一点,谢谢,我想我可以查找差异,但它是什么?
loads
读取一个字符串object@melon
{“键”:1}
有效吗?我收到的消息是:'警告,无法分析行:'参数:('')@rakim是否检查空行?在开始阅读时,我会使用类似于
content=[l.strip()]的内容,如果l.strip()]
lines = rec.split("\n")
rec = ''
size = len(lines)
i=0
for line in lines:
    try:
        self.on_data(json.loads(line))
    except:
        logging.warning ('warning, could not parse line:', line)
        if i == size - 1:
            # if it is the last element, we can keep it, since it might not be complete
            rec+=line
    finally:
        i += 1