0

I am using a magnetic card reader to read input from magnetic cards. The cards have three tracks, but each track holds the same exact data on each track. When I swipe a card, the first track is written to a variable and then written to a csv file for later use. The problem is that after each track is read, a return character is entered so that the while loop I am running reads and writes the same data three times for each swiped card. Is there any way that I can make the first two returns null so that I only get the data written once or is there any other way that I can go about doing this so that I only get the text once and data written once for each card? If you have any questions about this let me know. I tried to word it is clearly as possible. I will also include the code.

    #Card Reader
    import getpass
    import datetime
    import csv
    import os
    full_date = str(datetime.datetime.now())
    short_date = full_date[5:10]
    print(short_date)
    while True:
        while True:
            try:
                ID = getpass.getpass(prompt = 'Please swipe your ID card. ')
                if os.path.exists('.\attendance.csv'):
                    student_number = open('.\attendance.csv', 'a')
                    student_number.write(ID[1:10] + '\n')
                    print('')
                else:
                    student_number = open('.\attendance.csv', 'w')
                    student_number.write(ID[1:10] + '\n')
                    print('')
            except KeyboardInterrupt:
                break
law10
  • 13
  • 1
  • 4
  • Why the double `while True` loops? – Juxhin Sep 01 '15 at 18:20
  • I had more code in between at one point but took it out and just never caught it. I was going to clean it up later. So, there is no reason for it. Thanks for catching that though! – law10 Sep 01 '15 at 18:24
  • If the values are identical and of same length, why not grab the list of values and break after `n` index where `n` is the length of the desired value? – Juxhin Sep 01 '15 at 18:30

1 Answers1

1

You can use a counter to keep track of the number of times the value has been returned.

counter = 0
while True:
    ID = getpass.getpass(prompt = 'Please swipe your ID card.')
    if counter < 3:
        counter += 1
    else:
        counter = 0
        if os.path.exists('.\attendance.csv'):
            student_number = open('.\attendance.csv', 'a')
            student_number.write(ID[1:10] + '\n')
            print('')
        else:
            student_number = open('.\attendance.csv', 'w')
            student_number.write(ID[1:10] + '\n')
            print('')

Since you are developing a card reader, it is safe to assume that another card swipe will not interfere with the counter and each student's attendance would be written only once.

Divij Bindlish
  • 325
  • 1
  • 8
  • I was thinking of something like this when trying to solve it myself. I tried implementing it and then tried implementing yours also. Unfortunately this did not fix the problem. It is probably my fault for not explaining well. When I swipe a magnetic card it does the following: 1. Outputs a string of text (I believe 12 or 13 characters long) 2. The card (or card reader, I am not sure which is doing this) enters a return character 3. The same text in the first string is printed again on the next line because of the return character entered 4. Steps 2 and 3 are then repeated a second time. – law10 Sep 03 '15 at 17:42
  • Because of this, the program ends up running three times and recording the number 3 times for each card instead of just once. Is there a way that I can make it stop reading after the first track or somehow make the return characters null so that all 3 tracks are recorded as one variable? – law10 Sep 03 '15 at 17:44