[英]Python : seperate particular string from group of strings
Im working on a GPS tracker. 我正在使用GPS追踪器。 The tracker send some packets.
跟踪器发送一些数据包。 each packet start with start bit(7878) and stop bit(0d0a).
每个数据包均以起始位(7878)和终止位(0d0a)开始。 Some time i receive group of packets.
有一段时间我收到一组数据包。 I want to seperate those group of packets.
我想分开那组数据包。 I use below code to split the two char.
我使用下面的代码拆分两个字符。
data='78780a1301000400020017e73c0d0a787825260f071a0b050ac70165eaf0089b626021052e09019428139100fa7108000403020004a4810d0a78780a1301000409020016e73c0d0a'
splt_data= [data[i:i+2] for i in range(0, len(data), 2)]
i want like below 我想像下面
78780a1301000400020017e73c0d0a
787825260f071a0b050ac70165eaf0089b626021052e09019428139100fa7108000403020004a4810d0a
78780a1301000409020016e73c0d0a
I can able split those packets if "0a or 0d or 78 or" not inbetween start bit and stop bit. 如果“ 0a或0d或78或”不在起始位和终止位之间,则可以拆分这些数据包。 if there is possiblity of getting start bit or stop bit itself how to seperate it.
如果有可能获得开始位或停止位本身,如何将其分开。 For example below is one of the packet, which contains "0a" inbetween start bit(7878) and stop bit(0d0a).
例如,下面是数据包之一,它在开始位(7878)和停止位(0d0a)之间包含“ 0a”。
7878ka130100**0a**00020017e73c0d0a
Thanks 谢谢
You can use regular expression. 您可以使用正则表达式。
re.findall
will return all the occurrences that match the pattern: re.findall
将返回所有与模式匹配的事件:
>>> data = '78780a1301000400020017e73c0d0a787825260f071a0b050ac70165eaf0089b626021052e09019428139100fa7108000403020004a4810d0a78780a1301000409020016e73c0d0a'
>>> import re
>>> re.findall('7878.*?0d0a', data)
['78780a1301000400020017e73c0d0a',
'787825260f071a0b050ac70165eaf0089b626021052e09019428139100fa7108000403020004a4810d0a',
'78780a1301000409020016e73c0d0a']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.