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