使用 re.search 在 sys.stdin.readline() 变化时主动过滤
Using re.search to actively filter sys.stdin.readline() as it changes
所以我对使用 python 还很陌生,但我有一些数据不断地通过管道传输到 python 脚本,该脚本从 sys.stdin.readline() 读取信息,然后使用 re.search 来过滤特定的信息位。问题是它只读取来的那串信息然后就退出了
while True:
the_line = sys.stdin.readline()
m = re.search(',"data":"(.+?)}]}', the_line)
if m:
print (m.group(1))
示例输入(抱歉,我知道它很乱)
stat update: {"stat":{"time":"2018-02-03 19:37:59 GMT","lati":6.81661,"long":- 58.11185,"alti":0,"rxnb":0,"rxok":0,"rxfw":0,"ackr":0.0,"dwnb":0,"txnb":0,"pfrm":"Single Channel Gateway","mail":"kevic.lall@yahoo.com","desc":"433 MHz gateway test project 1.0"}}
Packet RSSI: -56, RSSI: -97, SNR: 9, Length: 10
rxpk update: {"rxpk": [{"tmst":4153364745,"chan":0,"rfch":0,"freq":433.000000,"stat":1,"modu":"LORA" ,"datr":"SF7BW125","codr":"4/5","lsnr":9,"rssi":- 56,"size":10,"data":"aGVsbG8gMzA1Nw=="}]}
Packet RSSI: -49, RSSI: -96, SNR: 9, Length: 10
rxpk update: {"rxpk":[{"tmst":4155404009,"chan":0,"rfch":0,"freq":433.000000,"stat":1,"modu":"LORA","datr":"SF7BW125","codr":"4/5","lsnr":9,"rssi":-49,"size":10,"data":"aGVsbG8gMzA1OA=="}]}
Packet RSSI: -51, RSSI: -97, SNR: 9, Length: 10
....
这些只是不断流式传输的内容中的几行。
注意输入并没有像这里那样出现,而是随着我正在传送到 python 脚本的程序继续到 运行[=36= 逐行出现]
因此,我想要的输出应该是
aGVsbG8gMzA1Nw=="
aGVsbG8gMzA1OA=="
....
持续播放
但相反,我没有打印任何东西,程序只是挂起,直到我手动按下 Ctrl+C
第一个字符串只是退出,因为它不包含所需的信息,即使我确实更改它以过滤那里的东西,它也会打印我想要的输出然后存在并停止通过管道传输到 python脚本以及退出时
有没有更有效的方式来读取和过滤信息?我还能使用 re.search 功能吗?
此外,我使用 sys.stdin.realine() 逐行阅读它的原因是因为我想过滤每一行以通过 MQTT
发送
为清楚起见进行了编辑
我没有看到相同的行为。
import sys
import re
while True:
the_line = sys.stdin.readline()
m = re.search('he(.+?)you', the_line)
if m:
print(m.group(1))
我运行程序。系统提示我键入并按回车键。您的正则表达式已针对我输入的任何内容进行了测试。打印匹配的图案。 然后,我的提示再次返回给我。然后我可以重新输入另一个随机字符串。该程序不会为我停止。从你的代码来看,程序应该没有理由结束。
您的代码相当高效。还有其他方法可以在 Python 中提示输入,就像有其他搜索字符串的方法一样。查看:
- How do you read from stdin in Python?
- How do you read from stdin in python from a pipe which has no ending
- What's a faster operation, re.match/search or str.find?
这取决于您要搜索的内容;如果你正在搜索变化的模式,你不会比 re.search()
快多少。但是,如果您知道确切的短语,或者如果您正在寻找一小部分确切的短语,string.find()
或 in
运算符可能会更快。
使用这种模式尝试一下:(?<="data":")[\w=]+(?=")
import sys
import re
regex = r'(?<="data":")[\w=]+(?=")'
while True:
text = sys.stdin.readline()
matches = re.finditer(regex, text)
for match in matches:
print ("{match}".format(match = match.group()))
以下脚本稍作修改,对我来说效果很好:
import fileinput
import re
for the_line in fileinput.input():
m = re.search(',"data":"(.+?)}]}', the_line)
if m:
print (m.group(1))
输出:
aGVsbG8gMzA1Nw=="
aGVsbG8gMzA1OA=="
所以我对使用 python 还很陌生,但我有一些数据不断地通过管道传输到 python 脚本,该脚本从 sys.stdin.readline() 读取信息,然后使用 re.search 来过滤特定的信息位。问题是它只读取来的那串信息然后就退出了
while True:
the_line = sys.stdin.readline()
m = re.search(',"data":"(.+?)}]}', the_line)
if m:
print (m.group(1))
示例输入(抱歉,我知道它很乱)
stat update: {"stat":{"time":"2018-02-03 19:37:59 GMT","lati":6.81661,"long":- 58.11185,"alti":0,"rxnb":0,"rxok":0,"rxfw":0,"ackr":0.0,"dwnb":0,"txnb":0,"pfrm":"Single Channel Gateway","mail":"kevic.lall@yahoo.com","desc":"433 MHz gateway test project 1.0"}}
Packet RSSI: -56, RSSI: -97, SNR: 9, Length: 10
rxpk update: {"rxpk": [{"tmst":4153364745,"chan":0,"rfch":0,"freq":433.000000,"stat":1,"modu":"LORA" ,"datr":"SF7BW125","codr":"4/5","lsnr":9,"rssi":- 56,"size":10,"data":"aGVsbG8gMzA1Nw=="}]}
Packet RSSI: -49, RSSI: -96, SNR: 9, Length: 10
rxpk update: {"rxpk":[{"tmst":4155404009,"chan":0,"rfch":0,"freq":433.000000,"stat":1,"modu":"LORA","datr":"SF7BW125","codr":"4/5","lsnr":9,"rssi":-49,"size":10,"data":"aGVsbG8gMzA1OA=="}]}
Packet RSSI: -51, RSSI: -97, SNR: 9, Length: 10
....
这些只是不断流式传输的内容中的几行。
注意输入并没有像这里那样出现,而是随着我正在传送到 python 脚本的程序继续到 运行[=36= 逐行出现]
因此,我想要的输出应该是
aGVsbG8gMzA1Nw=="
aGVsbG8gMzA1OA=="
....
持续播放
但相反,我没有打印任何东西,程序只是挂起,直到我手动按下 Ctrl+C
第一个字符串只是退出,因为它不包含所需的信息,即使我确实更改它以过滤那里的东西,它也会打印我想要的输出然后存在并停止通过管道传输到 python脚本以及退出时 有没有更有效的方式来读取和过滤信息?我还能使用 re.search 功能吗?
此外,我使用 sys.stdin.realine() 逐行阅读它的原因是因为我想过滤每一行以通过 MQTT
发送为清楚起见进行了编辑
我没有看到相同的行为。
import sys
import re
while True:
the_line = sys.stdin.readline()
m = re.search('he(.+?)you', the_line)
if m:
print(m.group(1))
我运行程序。系统提示我键入并按回车键。您的正则表达式已针对我输入的任何内容进行了测试。打印匹配的图案。 然后,我的提示再次返回给我。然后我可以重新输入另一个随机字符串。该程序不会为我停止。从你的代码来看,程序应该没有理由结束。
您的代码相当高效。还有其他方法可以在 Python 中提示输入,就像有其他搜索字符串的方法一样。查看:
- How do you read from stdin in Python?
- How do you read from stdin in python from a pipe which has no ending
- What's a faster operation, re.match/search or str.find?
这取决于您要搜索的内容;如果你正在搜索变化的模式,你不会比 re.search()
快多少。但是,如果您知道确切的短语,或者如果您正在寻找一小部分确切的短语,string.find()
或 in
运算符可能会更快。
使用这种模式尝试一下:(?<="data":")[\w=]+(?=")
import sys
import re
regex = r'(?<="data":")[\w=]+(?=")'
while True:
text = sys.stdin.readline()
matches = re.finditer(regex, text)
for match in matches:
print ("{match}".format(match = match.group()))
以下脚本稍作修改,对我来说效果很好:
import fileinput
import re
for the_line in fileinput.input():
m = re.search(',"data":"(.+?)}]}', the_line)
if m:
print (m.group(1))
输出:
aGVsbG8gMzA1Nw=="
aGVsbG8gMzA1OA=="