使用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