Python Forum
writting python output to a csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
writting python output to a csv file
#1
hi, i currently have a script that when a proximity fob is presented to the reader it displays the fob number (PIGPIO wiegand script on a raspberry pi)

i would like to make a python script so when i run it and then present the access fob it automatically puts the output into the next un-used column of the csv file.

can anyone help please

current script is as follows

-------------------------


#!/usr/bin/env python

import pigpio, csv

FILE_TO_READ = '/home/pi/furniss_acu/database/database.csv'
CSV_ID_KEY = 'Number :'

class decoder:

   """
   A class to read Wiegand codes of an arbitrary length.

   The code length and value are returned.

   EXAMPLE

   #!/usr/bin/env python

   import time

   import pigpio

   import wiegand

   def callback(bits, code):
      print("bits={} code={}".format(bits, code))

   pi = pigpio.pi()

   w = wiegand.decoder(pi, 14, 15, callback)

   while True:   
      time.sleep(60)

   w.cancel()

   pi.stop()
   """

   def __init__(self, pi, gpio_0, gpio_1, callback, bit_timeout=5):

      """
      Instantiate with the pi, gpio for 0 (green wire), the gpio for 1
      (white wire), the callback function, and the bit timeout in
      milliseconds which indicates the end of a code.

      The callback is passed the code length in bits and the value.
      """

      self.pi = pi
      self.gpio_0 = gpio_0
      self.gpio_1 = gpio_1

      self.callback = callback

      self.bit_timeout = bit_timeout

      self.in_code = False

      self.pi.set_mode(gpio_0, pigpio.INPUT)
      self.pi.set_mode(gpio_1, pigpio.INPUT)

      self.pi.set_pull_up_down(gpio_0, pigpio.PUD_UP)
      self.pi.set_pull_up_down(gpio_1, pigpio.PUD_UP)

      self.cb_0 = self.pi.callback(gpio_0, pigpio.FALLING_EDGE, self._cb)
      self.cb_1 = self.pi.callback(gpio_1, pigpio.FALLING_EDGE, self._cb)

   def _cb(self, gpio, level, tick):

      """
      Accumulate bits until both gpios 0 and 1 timeout.
      """

      if level < pigpio.TIMEOUT:

         if self.in_code == False:
            self.bits = 1
            self.num = 0

            self.in_code = True
            self.code_timeout = 0
            self.pi.set_watchdog(self.gpio_0, self.bit_timeout)
            self.pi.set_watchdog(self.gpio_1, self.bit_timeout)
         else:
            self.bits += 1
            self.num = self.num << 1

         if gpio == self.gpio_0:
            self.code_timeout = self.code_timeout & 2 # clear gpio 0 timeout
         else:
            self.code_timeout = self.code_timeout & 1 # clear gpio 1 timeout
            self.num = self.num | 1

      else:

         if self.in_code:

            if gpio == self.gpio_0:
               self.code_timeout = self.code_timeout | 1 # timeout gpio 0
            else:
               self.code_timeout = self.code_timeout | 2 # timeout gpio 1

            if self.code_timeout == 3: # both gpios timed out
               self.pi.set_watchdog(self.gpio_0, 0)
               self.pi.set_watchdog(self.gpio_1, 0)
               self.in_code = False
               self.callback(self.bits, self.num)

   def cancel(self):

      """
      Cancel the Wiegand decoder.
      """

      self.cb_0.cancel()
      self.cb_1.cancel()


def readFile(filename):
   data = []
   with open(filename, 'r') as file:
      reader = csv.DictReader(file)
      data = [row for row in reader]
   return data

def authenticateFromFile(id):
   for row in readFile(FILE_TO_READ):
      if CSV_ID_KEY in row and row[CSV_ID_KEY] == id:
         return True
   return False

if __name__ == "__main__":

   import time
   import RPi.GPIO as GPIO
   import pigpio
   import wiegand

   GPIO.setmode(GPIO.BCM)
   GPIO.setwarnings(False)

   def callback(bits, value):
      if authenticateFromFile('{}'.format(value)):
         print("Wiegand bits={} Card Number={}".format(bits, value))
         file = open("/home/pi/ACU/www/events.txt", "w")
         file.write("Last Access: Wiegand bits={}\n".format(bits))
         file.write("\nCard value={}".format(value))
         GPIO.setup(18,GPIO.OUT)
         print("door released")
         GPIO.output(18,GPIO.LOW)
         time.sleep(5)
         print("door secure")
         GPIO.output(18, GPIO.HIGH)
      else:
         print("Access Denied = Wiegand bits={} Card Number={}".format(bits, value))
         file = open("/home/pi/ACU/www/events.txt", "w")
         file.write("Access Denied = Wiegand bits={} Card Number={}".format(bits, value))



   pi = pigpio.pi()

   w = wiegand.decoder(pi, 14, 15, callback)



   while True:
      time.sleep(60)

   w.cancel()

   pi.stop()
Display posts from previous:
Sort by
Reply
#2
What have you tried? How much Python do you know?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,088 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
Photo how I write the output into xml file in python? 3lnyn0 1 2,219 Oct-31-2021, 05:40 PM
Last Post: Gribouillis
  Showing and saving the output of a python file run through bash Rim 3 2,419 Oct-06-2021, 10:48 AM
Last Post: gerpark
  Output CSV file with filepath and file contents glittergirl 1 1,746 Aug-03-2020, 01:50 AM
Last Post: glittergirl
  Python interpreter - output to file SectionProperties 8 5,489 Apr-11-2020, 01:38 PM
Last Post: SectionProperties
  csv file output rturus 7 3,214 Jan-30-2020, 02:09 PM
Last Post: buran
  File name as part of output file name Jeeping_Coder 1 2,108 Jan-10-2020, 03:43 PM
Last Post: Clunk_Head
  Writting to file from if/else statement mcmxl22 2 1,866 Nov-19-2019, 06:23 PM
Last Post: mcmxl22
  How to extract a matrix from .xml.gz file to a excel file or any other output? enyrb 0 2,052 Oct-21-2019, 01:01 PM
Last Post: enyrb
  python file output to log file Rsh 4 3,676 Aug-13-2019, 09:00 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020