mardi 19 janvier 2021

searching substring in a string using if statement behaves differently in a same-like variable types

I am posting two pieces of codes that do search in a file based on the user's input and print out matching output among other values.

code (1) uses search by name, and code(2) uses search by mac. in the case of code(1), it does not return any matches while in code(2) it returns matches properly although all variables types are (string)!

Code(1)

searchbyname = input("Enter Device Name: ").lower()
with open(r"C:\Users\afahmy\Desktop\final2.txt", "r") as output_file:
    for line in output_file:
        ip = line.split(',')[0]
        password = line.split(',')[1]
        mac = line.split(',')[2].rstrip()
        name = line.split(',')[3].rstrip()
        if searchbyname in name:
            print(format("IP: " + ip + "\n" + "Password: " + password + "\n" + "Mac Address: " + mac + "\n" + "Device Name: " + name))

Code(2)

searchbymac = input("Enter mac-address: ").lower().replace(':', '')
with open(r"C:\Users\afahmy\Desktop\final2.txt", "r") as output_file:
    for line in output_file:
        ip = line.split(',')[0]
        password = line.split(',')[1]
        mac = line.split(',')[2].rstrip()
        name = line.split(',')[3].rstrip()
        if searchbymac in mac:
            print(format("IP: " + ip + "\n" + "Password: " + password + "\n" + "Mac Address: " + mac + "\n" + "Device Name: " + name))

Output when running the script that has the two codes

Enter Device Name: Test Phone - VVX411 << nothing returned and skip to the second piece of code
Enter mac-address: 64167f185bf3
IP: 10.10.10.10.5
Password: Password!
Mac Address: 64167f185bf3
Device Name: Test phone - VVX411

Process finished with exit code 0

Any explanation or any light on what might be wrong in the code?

Aucun commentaire:

Enregistrer un commentaire