我试图在文件中找到一个等于特定MAC地址的特定范围。
以下是代码:
sensortag=0
while sensortag != "B4:99:4C:64:33:E0":
os.system("hcitool lescan> scan.txt & pkill --signal SIGINT hcitool")
scan = open("scan.txt", "r")
readscan = scan.read()
#read range 40-56 in file, NOTE: THIS WORKS IF I JUST KEEP IT if readscan[40] == "B", b being the start of the MAC address
if readscan[40:56] == "B4:99:4C:64:33:E0":
print "SensorTag found."
sensortag = "B4:99:4C:64:33:E0"
代码无限循环。
更新:感谢jkalden我的代码现在可以使用此解决方法:
if "B4:99:4C:64:33:E0" in readscan:
print "SensorTag found."
sensortag = "B4:99:4C:64:33:E0"
我使用for循环打印索引号和相应的值,以验证它是我需要的40-56范围。
for index, i in enumerate(readscan):
print index, i
答案 0 :(得分:0)
问题是你在循环没有结束的时候。试试这样的事情
os.system("hcitool lescan> scan.txt & pkill --signal SIGINT hcitool")
found = False
with open('scan.txt') as fin:
for line in fin:
if line[40:56] == 'B4:99:4C:64:33:E0':
found = True
break
if found:
print "SensorTag found."
答案 1 :(得分:0)
import re
os.system("hcitool lescan> scan.txt & pkill --signal SIGINT hcitool")
scan = open("scan.txt", "r")
readscan = scan.read()
all_Mac = re.findall("([0-9A-F]{2}(?::[0-9A-F]{2}){5})",readscan)
if 'B4:99:4C:64:33:E0' in all_Mac:
print "Sensor tag found"
我使用regex来查找所有macaddress
答案 2 :(得分:0)
感谢jkalden,我的代码现在可以使用此解决方法:
if "B4:99:4C:64:33:E0" in readscan:
print "SensorTag found."
sensortag = "B4:99:4C:64:33:E0"