我正在尝试通过函数接收Python中的字符串。
考虑以下事项;
我有一个在Python中运行的脚本。该脚本连续运行。它绑定到USB端口并侦听传入的ZigBee数据帧。
我有一个函数可以解析这个数据帧;
# Decoded data frame to use later on
def decodeReceivedFrame(data):
source_addr_long = toHex(data['source_addr_long'])
source_addr = toHex(data['source_addr'])
id = data['id']
rf_data = data['rf_data']
#samples = data['samples']
return [source_addr_long, source_addr, id, rf_data]
我以后打印此功能时;它给了我正确的传入值。例如;
decodedData = decodeReceivedFrame(data)
print decodedData
输出:
[None, None, 'rx', '<=>\x80\x02#387239766#XBEE#126#STR:wm2 #STR:c47cb3f57365#']
我想要做的是提取此字符串的两个STR变量。这意味着wm2字符串和c47cb3f57365字符串,分为两个单独的变量。
Python中哪个函数最有效解决这种情况?
答案 0 :(得分:3)
假设数据始终采用评论中讨论的格式,这将是最有效的方式之一:
s = '<=>\x80\x02#387239766#XBEE#126#STR:wm2 #STR:c47cb3f57365#'
# rsplit with 2 as the second arg will split twice on #STR starting from the send of s
spl = s.rsplit("#STR:",2)
# get last two elements from spl
a,b = spl[1],spl[2][:-1] # ,[:-1] removes the final #
print a,b
wm2 c47cb3f57365
使用ipython和timeit的一些时间:
In [6]: timeit re.findall(r'STR:(\w+)', s)
1000000 loops, best of 3: 1.67 µs per loop
In [7]: %%timeit
spl = s.rsplit("#STR:",2)
a,b = spl[1],spl[2][:-1]
...:
1000000 loops, best of 3: 409 ns per loop
如果你要使用正则表达式,你应该编译:
patt = re.compile(r'STR:(\w+)')
patt.findall(s)
这提高了效率:
In [6]: timeit patt.findall(s)
1000000 loops, best of 3: 945 ns per loop
答案 1 :(得分:1)
>>> import re
>>> re.findall(r'STR:(\w+)', '<=>\x80\x02#387239766#XBEE#126#STR:wm2 #STR:c47cb3f57365#')
['wm2', 'c47cb3f57365']