如何过滤字符以比较 Python 中的字符串

How to filter characters for comparison of strings in Python

我是编码新手,我不熟悉 python,你们能不能给我一个小例子,说明你们将如何解决这个问题。 基本上,我将要使用的这个新设备有一个二维码(类似条形码之类的东西),当我用二维扫描仪扫描二维码时,我的记事本上会出现这样的字符串,例如:58183#99AF0M000F9EF3F800 最后 12 个字符是 MAC 地址,前 5 个字符是订单号。

我需要将那个 (58183#99AF0M000F9EF3F800) 与我从 XML 页面获得的 MAC 地址值进行比较。 这是更多参考的终端输出:

####################################################################################################
Current device information:
Order-number:  58184 Software-version:  1.0.0 ( Build : 1 ) Hardware version:  1.00 MAC address:  00:0F:9E:F4:1A:80
desired-Order-number: 58183 desired-Softwareversion: 1.0.0 ( Build : 1 ) desired-hardwareversion: 1.00  pc-praefix: 7A2F7
PASS
PS C:\Users\Aswin\Python Project> 

XML 页面的 MAC 地址看起来像这样“00:0F:9E:F4:1A:80”,二维扫描码像这样“58183#99AF0M000F9EF3F800”。我如何获取此扫描码的最后 12 个字符并将其与 XML 页面中的 mac 地址进行比较以查看它们是否匹配。

任何代码块示例都将不胜感激。

try:
    preflash = urllib.request.urlopen("http://10.10.10.2", timeout=3).getcode()
    print("Web page status code:", preflash, "FAIL")  
    sys.exit(0)
    
except urllib.error.URLError:
    correct = urllib.request.urlopen("http://192.168.100.5", timeout=10).getcode()
    print("Web page status code:", correct)
    print("IP address: 192.168.100.5 is reachable")
    

print(100*"#")

# Declare url String    
url_str = 'http://192.168.100.2/globals.xml'

# open webpage and read values
xml_str = urllib.request.urlopen(url_str).read()

# Parses XML doc to String for Terminal output
xmldoc = minidom.parseString(xml_str)

# prints the order_number from the xmldoc
order_number = xmldoc.getElementsByTagName('order_number')
ord_nmr = order_number[0].firstChild.nodeValue

# prints the firmware_version from the xmldoc
firmware_version = xmldoc.getElementsByTagName('firmware_version')
frm_ver = firmware_version[0].firstChild.nodeValue

# prints the hardware_version from the xmldoc
hardware_version = xmldoc.getElementsByTagName('hardware_version')
hrd_ver = hardware_version[0].firstChild.nodeValue
v = hrd_ver.split()[-1]

# prints the mac_address from the xmldoc
mac_address = xmldoc.getElementsByTagName('mac_address')
mac_addr = mac_address[0].firstChild.nodeValue

print("Current device information: ")
print("Order-number: ",ord_nmr, "Software-version: ",frm_ver, "Hardware version: ",v, "MAC address: ",mac_addr)

d_ordernum = "58183"
d_hw_version = "1.00"
d_sf_version = "1.0.0 ( Build : 1 )"
pc_praefix = "7A2F7"

print("desired-Order-number: 58183 desired-Softwareversion: 1.0.0 ( Build : 1 ) desired-hardwareversion: 1.00  pc-praefix: 7A2F7")

if d_sf_version == frm_ver:
    print("PASS")
else:
    print("FAIL")

您可以从扫描码中取出字符串并将其切片

scan_code_cropped = scancode_string[11:]

这将为您获取扫描码的最后 12 个字符。

现在要获取格式为 MAC 的地址,以便能够将其与扫描码进行比较,在“:”的基础上拆分它

list_of_chars = mac_address_string.split(":")

这将为您提供字符列表,可以使用

将其连接起来
mac_address_string_joined = ''.join(list_of_chars) 

最后比较两个字符串

if scan_code_cropped == mac_address_string_joined:
    print("Mac address & Scan code matched !")

如果需要函数格式,请执行以下操作:

def match_scan_with_mac(scancode_string, mac_address_string):

    # get the last 12 characters of the scan code
    scan_code_cropped = scancode_string[11:]
    
    # get the mac address without the ":"
    list_of_chars = mac_address_string.split(":")
    mac_address_string_joined = ''.join(list_of_chars) 

    # compare the MAC address and the scan string
    if scan_code_cropped == mac_address_string_joined:
        print("Mac address & Scan code matched !")
        return True

    return False