将数据从python脚本管道传输到.csv或.txt文件

问题描述:

我想监视我公寓内的空气质量(楼下的邻居是个狂热的吸烟者).为此,我将Raspberry Pie 3 Model B +与Enviro +连接.

I want to monitor air quality inside my flat (downstairs neighbor is an avid smoker). For so doing, I have a Raspberry Pie 3 Model B+ connected with an Enviro+.

在终端中,我可以运行用于测量小颗粒的脚本. python particulates.py参见 https://github.com/pimoroni/enviroplus-python/blob/master/examples/particulates.py

While in the terminal I can run the script that measures small particles. python particulates.py cf. https://github.com/pimoroni/enviroplus-python/blob/master/examples/particulates.py

然后它运行并在终端中显示数据,如下图所示:(是的,我更乐于学习,而不是编码).

It then runs and display the data in the terminal, like in the picture below: (yes, I am better at lego-ing, than coding).

我有兴趣在文本文件或csv中记录数据,因此我在考虑是否有一种方法可以将屏幕上显示的数据直接传输到csv或txt文件中.我知道,我可能可以编写一个简单且精巧的Python脚本,但是我听到了很多Linux的赞美,我想尝试一下.此外,我在一个论坛上看到:

I am interested in keeping a record of the data in a text files or a csv, so I was thinking if there is a way to pipe the data that is shown on the screen directly into a csv or txt files. I know, I could probably write a simple Python script that makes it nice and tight, but I heard many praises of pipping in Linux, that I wanted to give it a try. Moreover, I saw in one forum that:

Awk Sed 是您的Shell脚本朋友. 在Linux中,所有内容都是文件,Awk和Sed可以写入/编辑文件. 每个Linux操作系统附带的可怕的旧东西都可以正常工作.

Awk and Sed are your Shell script friends. In Linux everything is a file, Awk and Sed can write/edit files. Horrible old stuff that comes with every Linux OS and just works.

如果您使用Web服务器,则无需所有x11桌面膨胀功能,只需一个无头的Linux操作系统即可. 轻松将所有这些安装在128MB卡上. Busybox有一个Web服务器,或者您可以从许多选项中进行选择. 我使用Shell脚本和Awk/Sed即时重写CGI/HTML文件.

If you use a webserver then no need for all that x11 desktop bloat stuff, just a headless Linux OS. Easy fit all this on a 128MB card. Busybox has a webserver or you can pick from a bunch of options. I use shell script and Awk/Sed to rewrite the CGI/HTML files on the fly.

来源: https://www.raspberrypi.org/forums/viewtopic .php?t = 248733

我在终端中输入了以下内容:

I have typed the following in the terminal:

python Particulates.py > data.csv

我也尝试过:

python Particulates.py | data.csv

数据显示在屏幕上,创建了一个csv文件,但两个选项的data.csv为空.如果我的理解是正确的,则需要将particles.py的标准输出放入data.csv.那里应该缺少一些东西,因为我得到的与这位awk/sed倡导者在论坛中做广告的目标相去甚远.

The data were displayed on the screen, a csv file was created, but the data.csv was empty for both options. If my understanding is correct, I need to put the stdout of the particulates.py into the data.csv. There ought to be something missing in there, because, what I get is far from what this awk/sed advocate was advertising in the forum.

有什么线索可以完成这项工作吗?如果我要绘制结果,我应该使用gnuplot吗?我可以在同一条线中使用它,就像在一条管道中使用吗?

Any clue how I could get this done? and if I were to plot the results shall I use gnuplot ? can I use it in the same line, like in a chain of pipe?

谢谢您的帮助.

检查库代码

Check the library code https://github.com/pimoroni/pms5003-python/blob/master/library/pms5003/init.py#L66 to understand which values goes in each columns:

    def __repr__(self):
        return """
PM1.0 ug/m3 (ultrafine particles):                             {}
PM2.5 ug/m3 (combustion particles, organic compounds, metals): {}
PM10 ug/m3  (dust, pollen, mould spores):                      {}
PM1.0 ug/m3 (atmos env):                                       {}
PM2.5 ug/m3 (atmos env):                                       {}
PM10 ug/m3 (atmos env):                                        {}
>0.3um in 0.1L air:                                            {}
>0.5um in 0.1L air:                                            {}
>1.0um in 0.1L air:                                            {}
>2.5um in 0.1L air:                                            {}
>5.0um in 0.1L air:                                            {}
>10um in 0.1L air:                                             {}
""".format(*self.data[:-2], checksum=self.checksum)

并使用此数据编写CSV:

and use this data to write CSV:

import csv
from datetime import datetime


pms5003 = PMS5003()
time.sleep(1.0)

try:
    while True:
        try:
            with open('pms5003.csv', 'a', newline='') as csvfile:
                writer = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
                data = pms5003.read()  # PMS5003Data instance
                dt = datetime.now()
                writer.writerow([dt.isoformat()] + list(data.data[:-2]))
        except ReadTimeoutError:
            pms5003 = PMS5003()
except KeyboardInterrupt:
    pass