本文介绍了Python Regex:查找所有以“{"开头并以“}"结尾的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过套接字接收数据,一堆 JSON 字符串.但是,我收到了一定数量的字节,因此有时我的最后一个 JSON 字符串会被截断.我通常会得到以下信息:

{"pitch":-30.778193,"yaw":-124.63285,"roll":-8.977466}{"pitch":-30.856342,"yaw":-124.57556,"roll":-7.7220345}{"pitch":-31.574106,"yaw":-124.65623,"roll":-7.911794}{"pitch":-30.479567,"yaw":-124.24301,"roll":-8.730827}{"pitch":-29.30239,"yaw":-123.97949,"roll":-8.134723}{"pitch":-29.84712,"yaw":-124.584465,"roll":-8.588374}{"pitch":-31.072054,"yaw":-124.707466,"roll":-8.877062}{"pitch":-31.493435,"yaw":-124.75457,"roll":-9.019922}{"pitch":-29.591925,"yaw":-124.960815,"roll":-9.379437}{"pitch":-29.37105,"yaw":-125.14427,"roll":-9.642341}{"pitch":-29.483717,"yaw":-125.16528,"roll":-9.687177}{"pitch":-30.903332,"yaw":-124.603935,"roll":-9.423098}{"pitch":-30.211857,"yaw":-124.471664,"roll":-9.116135}{"pitch":-30.837414,"yaw":-125.18984,"roll":-9.824204}{"pitch":-30.526165,"yaw":-124.85788,"roll":-9.158611}{"pitch":-30.333513,"yaw":-123.68705,"roll":-7.9481263}{"pitch":-30.903502,"yaw":-123.78847,"roll":-8.209373}{"pitch":-31.194769,"yaw":-124.79708,"roll":-8.709783}{俯仰":-30.816765,偏航":-125

使用 Python,我想创建一个包含前 18 个完整 { data... } 字符串的字符串数组.

这是我尝试过的: cleanData = re.search('{.*}', data) 但这似乎只给了我第一个 { 数据... } 条目.如何获得完整的 { } 集合的完整字符串数组?

解决方案

要获取全部,可以使用 re.finditerre.findall.

>>>re.findall(r'{.*}', s)['{"pitch":-30.778193,"yaw":-124.63285,"roll":-8.977466}','{"pitch":-30.856342,"yaw":-124.57556,"roll":-7.72820, '{"pitch":-31.574106,"yaw":-124.65623,"roll":-7.911794}','{"pitch":-30.479567,"yaw":-124.24301,"roll":-8.730, '{"pitch":-29.30239,"yaw":-123.97949,"roll":-8.134723}','{"pitch":-29.84712,"yaw":-124.584465,"roll":-74}83, '{"pitch":-31.072054,"yaw":-124.707466,"roll":-8.877062}','{"pitch":-31.493435,"yaw":-124.75457,"roll":-22}0.0, '{"pitch":-29.591925,"yaw":-124.960815,"roll":-9.379437}','{"pitch":-29.37105,"yaw":-125.14427,"roll":-411}642, '{"pitch":-29.483717,"yaw":-125.16528,"roll":-9.687177}', '{"pitch":-30.903332,"yaw":-124.603935,"roll":-900}422, '{"pitch":-30.211857,"yaw":-124.471664,"roll":-9.116135}', '{"pitch":-30.837414,"yaw":-125.18984,"roll":-9.}82224., '{"pitch":-30.526165,"yaw":-124.85788,"roll":-9.158611}','{"pitch":-30.333513,"yaw":-123.68705,"roll":-2634}', '{"pitch":-30.903502,"yaw":-123.78847,"roll":-8.209373}', '{"pitch":-31.194769,"yaw":-124.79708,"roll":-8.709783}']>>>

>>>[x.group() for x in re.finditer(r'{.*}', s)]['{"pitch":-30.778193,"yaw":-124.63285,"roll":-8.977466}','{"pitch":-30.856342,"yaw":-124.57556,"roll":-7.72820, '{"pitch":-31.574106,"yaw":-124.65623,"roll":-7.911794}','{"pitch":-30.479567,"yaw":-124.24301,"roll":-8.730, '{"pitch":-29.30239,"yaw":-123.97949,"roll":-8.134723}','{"pitch":-29.84712,"yaw":-124.584465,"roll":-74}83, '{"pitch":-31.072054,"yaw":-124.707466,"roll":-8.877062}','{"pitch":-31.493435,"yaw":-124.75457,"roll":-22}0.0, '{"pitch":-29.591925,"yaw":-124.960815,"roll":-9.379437}','{"pitch":-29.37105,"yaw":-125.14427,"roll":-411}642, '{"pitch":-29.483717,"yaw":-125.16528,"roll":-9.687177}','{"pitch":-30.903332,"yaw":-124.603935,"roll":-900}422, '{"pitch":-30.211857,"yaw":-124.471664,"roll":-9.116135}', '{"pitch":-30.837414,"yaw":-125.18984,"roll":-9.}82224., '{"pitch":-30.526165,"yaw":-124.85788,"roll":-9.158611}','{"pitch":-30.333513,"yaw":-123.68705,"roll":-2634}', '{"pitch":-30.903502,"yaw":-123.78847,"roll":-8.209373}', '{"pitch":-31.194769,"yaw":-124.79708,"roll":-8.709783}']>>>

I am receiving data over a socket, a bunch of JSON strings. However, I receive a set amount of bytes, so sometimes the last of my JSON strings is cut-off. I will typically get the following:

{"pitch":-30.778193,"yaw":-124.63285,"roll":-8.977466}
{"pitch":-30.856342,"yaw":-124.57556,"roll":-7.7220345}
{"pitch":-31.574106,"yaw":-124.65623,"roll":-7.911794}
{"pitch":-30.479567,"yaw":-124.24301,"roll":-8.730827}
{"pitch":-29.30239,"yaw":-123.97949,"roll":-8.134723}
{"pitch":-29.84712,"yaw":-124.584465,"roll":-8.588374}
{"pitch":-31.072054,"yaw":-124.707466,"roll":-8.877062}
{"pitch":-31.493435,"yaw":-124.75457,"roll":-9.019922}
{"pitch":-29.591925,"yaw":-124.960815,"roll":-9.379437}
{"pitch":-29.37105,"yaw":-125.14427,"roll":-9.642341}
{"pitch":-29.483717,"yaw":-125.16528,"roll":-9.687177}
{"pitch":-30.903332,"yaw":-124.603935,"roll":-9.423098}
{"pitch":-30.211857,"yaw":-124.471664,"roll":-9.116135}
{"pitch":-30.837414,"yaw":-125.18984,"roll":-9.824204}
{"pitch":-30.526165,"yaw":-124.85788,"roll":-9.158611}
{"pitch":-30.333513,"yaw":-123.68705,"roll":-7.9481263}
{"pitch":-30.903502,"yaw":-123.78847,"roll":-8.209373}
{"pitch":-31.194769,"yaw":-124.79708,"roll":-8.709783}
{"pitch":-30.816765,"yaw":-125

With Python, I would like to create a string array of the first 18 complete { data... } strings.

Here is what I have tried: cleanData = re.search('{.*}', data) but it seems like this is only giving me the very first { data... } entry. How can I get the full string array of complete { } sets?

解决方案

To get all, you can use re.finditer or re.findall.

>>> re.findall(r'{.*}', s)
['{"pitch":-30.778193,"yaw":-124.63285,"roll":-8.977466}', '{"pitch":-30.856342,"yaw":-124.57556,"roll":-7.7220345}', '{"pitch":-31.574106,"yaw":-124.65623,"roll":-7.911794}', '{"pitch":-30.479567,"yaw":-124.24301,"roll":-8.730827}', '{"pitch":-29.30239,"yaw":-123.97949,"roll":-8.134723}', '{"pitch":-29.84712,"yaw":-124.584465,"roll":-8.588374}', '{"pitch":-31.072054,"yaw":-124.707466,"roll":-8.877062}', '{"pitch":-31.493435,"yaw":-124.75457,"roll":-9.019922}', '{"pitch":-29.591925,"yaw":-124.960815,"roll":-9.379437}', '{"pitch":-29.37105,"yaw":-125.14427,"roll":-9.642341}', '{"pitch":-29.483717,"yaw":-125.16528,"roll":-9.687177}', '{"pitch":-30.903332,"yaw":-124.603935,"roll":-9.423098}', '{"pitch":-30.211857,"yaw":-124.471664,"roll":-9.116135}', '{"pitch":-30.837414,"yaw":-125.18984,"roll":-9.824204}', '{"pitch":-30.526165,"yaw":-124.85788,"roll":-9.158611}', '{"pitch":-30.333513,"yaw":-123.68705,"roll":-7.9481263}', '{"pitch":-30.903502,"yaw":-123.78847,"roll":-8.209373}', '{"pitch":-31.194769,"yaw":-124.79708,"roll":-8.709783}']
>>> 

OR

>>> [x.group() for x in re.finditer(r'{.*}', s)]
['{"pitch":-30.778193,"yaw":-124.63285,"roll":-8.977466}', '{"pitch":-30.856342,"yaw":-124.57556,"roll":-7.7220345}', '{"pitch":-31.574106,"yaw":-124.65623,"roll":-7.911794}', '{"pitch":-30.479567,"yaw":-124.24301,"roll":-8.730827}', '{"pitch":-29.30239,"yaw":-123.97949,"roll":-8.134723}', '{"pitch":-29.84712,"yaw":-124.584465,"roll":-8.588374}', '{"pitch":-31.072054,"yaw":-124.707466,"roll":-8.877062}', '{"pitch":-31.493435,"yaw":-124.75457,"roll":-9.019922}', '{"pitch":-29.591925,"yaw":-124.960815,"roll":-9.379437}', '{"pitch":-29.37105,"yaw":-125.14427,"roll":-9.642341}', '{"pitch":-29.483717,"yaw":-125.16528,"roll":-9.687177}', '{"pitch":-30.903332,"yaw":-124.603935,"roll":-9.423098}', '{"pitch":-30.211857,"yaw":-124.471664,"roll":-9.116135}', '{"pitch":-30.837414,"yaw":-125.18984,"roll":-9.824204}', '{"pitch":-30.526165,"yaw":-124.85788,"roll":-9.158611}', '{"pitch":-30.333513,"yaw":-123.68705,"roll":-7.9481263}', '{"pitch":-30.903502,"yaw":-123.78847,"roll":-8.209373}', '{"pitch":-31.194769,"yaw":-124.79708,"roll":-8.709783}']
>>> 

这篇关于Python Regex:查找所有以“{"开头并以“}"结尾的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 13:09