0

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?

3 Answers 3

3

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."
Sign up to request clarification or add additional context in comments.

2 Comments

Python has for...else form? That is neat. +1, Never heard of that
Hmm I will try the for else to see what happens. my file that I am opening contains 100 different id #. Each listed per line in the file. It is important I know which line contains the cardid. so if card id is the 13th one it will print "13".
2

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"

1 Comment

I'm trying to process the file until I find the card id and figure out what line it is on. I have another file which has a corresponding command. so card.txt has the cardids and command.txt has the corresponding commands. so line 1 (cardid) in card.txt will do command.txt line 1 (print "1") and so forth. I found this is the easiest way to manage 100 cardids and seperate commands.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.