简体   繁体   English

Python:将特定字符串与字符串组分开

[英]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']
  • 7878 , 0d0a matches 7878 , 0d0a literally. 78780d0a匹配78780d0a字面上。
  • . matches any character. 匹配任何字符。
  • .* matches 0 or more any (.) characters (greedy) . .*匹配0个或多个任何(。)字符(贪婪) Appending ? 追加? will make the match non-greedy. 会使比赛变得非贪婪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中的列表中分离字符串 - Seperate strings in a list in python 如何从文本文件Python 3中分离字符串中的字符 - How to seperate characters in a string from a textfile, Python 3 python从混合字符串中找到单独的字符串并提供空间 - python find seperate string from mixed string and providing space 从特定日期开始按月分组 python - Group by month from a particular date python 如何在python 3中的其他字符串列表中的元素中对匹配字符串的列表进行分组 - How to group in a list matching strings from elements from other string lists in python 3 完全匹配2个字符串,除了在python中存在特定字符串的地方 - match 2 strings exactly except at places where there is a particular string in python 从一组字符串中解析数字Python - Parsing number from a group of strings Python 如何在Python中的单个正则表达式中的正则表达式组中搜索特定字符串? - How to search for a particular string with in a regex group with in a single regex in Python? 在 python 中将具有空格的字符串与没有空格的数组分开 - Seperate strings from array that have whitespace from those that dont have whitespaces in python 如何将从binance api返回的交换对字符串分成两个字符串 - How to seperate an exchange pair string returned from binance api into two strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM