你如何合并 2 个 csv 文件的 raspberry pi python 代码?

How do you combine raspberry pi python code of 2 csv files?

我们正在向 space 发送一个带有 raspberry pi 感应帽和 rasp pi 相机 V2 的气象气球我们已经编写了代码以保存到文件

这是拍照的代码

from picamera import PiCamera
from time import sleep

camera = PiCamera()

camera.start_preview()

camera.start_preview(alpha=200)
camera.rotation = 180

for i in range(3):

    sleep(5)
    
    camera.capture('/home/pi/Desktop/image%s.jpg' % i)

camera.stop_preview()

这是感知环境的代码

from sense_hat import SenseHat
import time
sense=SenseHat()

file = open("Datafile.csv","a")

file.write("Time, Humidity, Temperature, Pressure")

print ("Time, Humidity, Temperature, Pressure")

for n in range(60 ):
    humidity = sense.get_humidity()
    humidity = round(humidity, 2)
    
    file.write(time.strftime('%X'))
    file.write(",") 
    file.write(str(humidity))
    file.write("/n")

    temperature = sense.get_temperature()
    temperature = round(temperature, 2)
    
    print (time.strftime('%X'),humidity)
    
    file.write(time.strftime('%X'))
    file.write(",") 
    file.write(str(temperature))
    file.write("/n")
    
    temperature = sense.get_temperature()
    temperature = round(temperature, 2)
    
    print (time.strftime('%X'),temperature)
    
    pressure = sense.get_pressure()
    pressure = round(pressure, 2)
    
    file.write(time.strftime('%X'))
    file.write(",") 
    file.write(str(pressure))
    file.write("\n")
    
    print (time.strftime('%X'),pressure)
    time.sleep(1)

file.close()

这两项工作我们现在正试图将它们结合起来。请帮忙!!

我要做的是为拍照和收集传感器数据的每个动作编写代码,并为每个动作创建一个函数。然后您可以使用调用每个函数的单个循环。我很快将一个示例放在一起,说明如何使用您的代码,该代码使用单个主循环大约每 5 秒收集一张照片,大约每秒收集一次传感器数据。

from picamera import PiCamera
from sense_hat import SenseHat
import time


def initialize_camera():
    camera = PiCamera()
    camera.start_preview()
    camera.start_preview(alpha=200)
    camera.rotation = 180
    return camera


def capture_picture(camera, img_number):
    camera.capture('/home/pi/Desktop/image%s.jpg' % img_number)


def capture_sensor_data(sense, file):
    humidity = sense.get_humidity()
    humidity = round(humidity, 2)

    file.write(time.strftime('%X'))
    file.write(",")
    file.write(str(humidity))
    file.write("/n")

    temperature = sense.get_temperature()
    temperature = round(temperature, 2)

    print(time.strftime('%X'), humidity)

    file.write(time.strftime('%X'))
    file.write(",")
    file.write(str(temperature))
    file.write("/n")

    temperature = sense.get_temperature()
    temperature = round(temperature, 2)

    print(time.strftime('%X'), temperature)

    pressure = sense.get_pressure()
    pressure = round(pressure, 2)

    file.write(time.strftime('%X'))
    file.write(",")
    file.write(str(pressure))
    file.write("\n")

    print(time.strftime('%X'), pressure)


if __name__ == '__main__':
    camera = initialize_camera()
    sense = SenseHat()

    with open("Datafile.csv", "a") as file:
        file.write("Time, Humidity, Temperature, Pressure")
        print("Time, Humidity, Temperature, Pressure")

        photo_num = 1

        for i in range(60):
            if i % 5 == 0:
                capture_picture(camera, photo_num)
                photo_num += 1

            capture_sensor_data(sense, file)
            time.sleep(1)

        camera.stop_preview()