社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

使用python解析json行

rakim • 6 年前 • 1322 次点击  

我有这种方法:

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看起来还好吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38888
 
1322 次点击  
文章 [ 1 ]  |  最新文章 6 年前
DirtyBit
Reply   •   1 楼
DirtyBit    6 年前

如评论中所述,您需要 self.on_data(json.loads(line)) :

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