python中的传感器数据日志csv

python中的传感器数据日志csv,python,csv,raspberry-pi,sensors,Python,Csv,Raspberry Pi,Sensors,我是编程新手,想为红外传感器编写一个代码,以便在检测到运动时将时间戳记录在.csv文件中。到目前为止,我已经找到了用于检测的代码,但现在需要添加代码来指定在csv文件中写入条目。运动检测代码的学分: 接下来,我尝试添加一些内容,这些内容将写入两列TIMESTAMP和“motiondetected”: 我只找到了从静态文件写入CSV的方法,所以它们似乎没有为我的问题提供一个简单的答案。因此,加入这些代码或纠正第二个代码的任何帮助都将是巨大的 import RPi.GPIO as GPIO impo

我是编程新手,想为红外传感器编写一个代码,以便在检测到运动时将时间戳记录在.csv文件中。到目前为止,我已经找到了用于检测的代码,但现在需要添加代码来指定在csv文件中写入条目。运动检测代码的学分:

接下来,我尝试添加一些内容,这些内容将写入两列TIMESTAMP和“motiondetected”:

我只找到了从静态文件写入CSV的方法,所以它们似乎没有为我的问题提供一个简单的答案。因此,加入这些代码或纠正第二个代码的任何帮助都将是巨大的

import RPi.GPIO as GPIO
import time
import csv
import strftime

GPIO.setmode(GPIO.BCM)
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)

def MOTION(PIR_PIN):
    print ("Motion Detected")

    print ("PIR Module Test (CTRL+C to exit)")

    row = [strfttime("%a, %d %b %Y %H:%M:%S"), 'motion_detected']    
    with open('datalog.csv', 'a') as f:
        w = csv.writer(f)
        w.writerow(row)

    time.sleep(2)
    print ("Ready")

try:
    GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
    while 1:
        time.sleep(100)

except KeyboardInterrupt:
    print("Quit")
    GPIO.cleanup()
注意:对于检测到的字符串
运动
,需要在其周围添加引号(在Python中,支持单引号和双引号)

import csv
import strftime 

row = [strfttime("%a, %d %b %Y %H:%M:%S"), motion_detected]    
with open('datalog.csv', 'a') as f:
    w = csv.writer(f)
    w.writerow(row)
import RPi.GPIO as GPIO
import time
import csv
import strftime

GPIO.setmode(GPIO.BCM)
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)

def MOTION(PIR_PIN):
    print ("Motion Detected")

    print ("PIR Module Test (CTRL+C to exit)")

    row = [strfttime("%a, %d %b %Y %H:%M:%S"), 'motion_detected']    
    with open('datalog.csv', 'a') as f:
        w = csv.writer(f)
        w.writerow(row)

    time.sleep(2)
    print ("Ready")

try:
    GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
    while 1:
        time.sleep(100)

except KeyboardInterrupt:
    print("Quit")
    GPIO.cleanup()