joegone
joegone

Reputation: 1

looking for a better solution to my script (python)

so I wasn't sure how to phrase this in a search so I will give you the details. I'm using an nfc reader and if a card is found an action is performed. What I want to do is add a part where if the card is not in the database print "unknown card"

heres my script essentially. max = sum of lines in a text file cardid = card uid #

for i in range(max)
    with open(....) as file
        card = file.readlines()[i]
    if cardid == card
        print "card found"

that is pretty much what i have so far. it works as expected. The only workaround I can see is adding a variable 'cardfound' and doing this

cardfound = 0
for i in range(max)
    with open(....) as file
        card = file.readlines()[i]
    if cardid == card
        print "card found"
        cardfound = 1
if cardfound == 0
     print "unknown card"

is there a better method?

Upvotes: 0

Views: 58

Answers (3)

Nagasaki45
Nagasaki45

Reputation: 2850

Looping through the lines of the list explicitly may be replaced with the in operator.

with open(...) as file:
    cards = file.readlines()
if card in cards:
    print('card found')
else:
    print('unknown card')

You may want to strip the '\n' from the end of each line, replacing the second line of the code above with:

cards = [line.strip() for line in file]

Lastly, if you just need to load the list of the cards once and perform checks against it many times use set instead of list (in will run much faster):

cards = set(line.strip() for line in file)

Upvotes: 0

aquavitae
aquavitae

Reputation: 19114

I assume that if a card is found, you don't need to continue processing the file? In that case you can do this:

with open(....) as file
    for card in file.readlines()
        if cardid == card
            print "card found"
            break
    else:
        print "unknown card"

Note that the with open() line is outside the loop, you don't really want to open the file every time. Also, you can do away with the range(max) since file is an iterable.

If you do want to process the entire file (e.g. counting the number of cards found, then the way you are doing it is probably best, although it would be better style to use a boolean:

cardfound = False
with open(....) as file
    for card in file.readlines()
        if cardid == card
            print "card found"
            cardfound = True
if not cardfound:
     print "No card found"

Update If you want to count the lines, then rather than manually iterating over each line, count them using enumerate:

cardfound = False
with open(....) as file
    for i, card in enumerate(file.readlines())
        if cardid == card
            print "card found on line %d" % i
            cardfound = True
if not cardfound:
     print "No card found"

Upvotes: 2

Amadan
Amadan

Reputation: 198314

First of all, you're reading the file over and over and over. Reading it once should suffice.

As for the rest - in most languages you would have to do basically what you did, have a flag somewhere that keeps track if you found something or not. However, Python has a cool for...else form:

with open(....) as file:
    for card in file.readlines():
        if card == cardid:
            print "OKAY!!!"
            break
    else:
        print "Sad now."

Upvotes: 3

Related Questions