如何使用python将变量增加到csv

时间:2017-11-24 19:35:38

标签: python raspberry-pi

您好我刚开始使用python和raspberry pi。 我有一个代码,根据我按下的按钮增加四个变量。 这些答案转到csv文件,它会显示按下按钮的时间以及自程序开始以来的次数。 这里的问题是csv文件只显示我按下按钮所以:1,0,0,0不是0 + n

有人可以告诉我为什么吗?谢谢!

这是我的代码:

import datetime
import pygame.mixer
import RPi.GPIO as GPIO
import datetime
import time
import csv

from pygame.mixer import Sound
from time import sleep
from datetime import date


with open("Csv_Test.csv", "a") as csv_file:
    csv_app = csv.writer(csv_file)
    csv_app.writerow(["Hour", "RealyGood", "Good", "bad", "RealyBad"])


    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(37,GPIO.IN, pull_up_down = GPIO.PUD_UP)
    GPIO.setup(35, GPIO.IN, pull_up_down = GPIO.PUD_UP)
    GPIO.setup(33, GPIO.IN, pull_up_down = GPIO.PUD_UP)
    GPIO.setup(31, GPIO.IN, pull_up_down = GPIO.PUD_UP)
    print("HelloWorld")

    hour = datetime.datetime.now().time()
    print(hour)
    pygame.mixer.init()
    drum = Sound("samples/drum_tom_mid_hard.wav")
    while True:
        heure = datetime.datetime.now().time()
        buttonRealyHappy = GPIO.input(37)   
        buttonHappy = GPIO.input(35)
        buttonbad = GPIO.input(33)
        buttonRealyBad = GPIO.input(31)
        printcsv = 0
        realyHappy =0
        happy =0
        bad = 0 
        realyBad = 0
        if buttonRealyHappy == 0:
            realyHappy += 1
            print("RealyHappy")
            drum.play()
            printcsv = 1
        if buttonHappy == 0:
            happy += 1
            print("Happy")
            drum.play()
            printcsv = 1
        if buttonBad == 0:
            bad += 1
            print("bad")
            drum.play()
            printcsv = 1
        if boutonRealyBad == 0:
            realyBad += 1
            print("Realybad")
            drum.play()
            printcsv = 1

        if (printcsv >  0):
            csv_app.writerow([happy, semi, mal, bad])
            printcsv = 0
            time.sleep(5)
    GPIO.cleanup()

0 个答案:

没有答案