从 Python 中的函数调用中提取变量
Extracting a variable from a function call in Python
我正在尝试提取函数接收到的 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 中的哪个函数可以最有效地解决这种情况?
假设数据始终采用评论中讨论的格式,这将是最有效的方法之一:
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
>>> import re
>>> re.findall(r'STR:(\w+)', '<=>\x80\x02#387239766#XBEE#126#STR:wm2 #STR:c47cb3f57365#')
['wm2', 'c47cb3f57365']
我正在尝试提取函数接收到的 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 中的哪个函数可以最有效地解决这种情况?
假设数据始终采用评论中讨论的格式,这将是最有效的方法之一:
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
>>> import re
>>> re.findall(r'STR:(\w+)', '<=>\x80\x02#387239766#XBEE#126#STR:wm2 #STR:c47cb3f57365#')
['wm2', 'c47cb3f57365']