Piping data from a python script into a .csv or .txt file

余生颓废 提交于 2020-04-17 19:01:08

问题


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+.

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). Text

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 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.

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.

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

I have typed the following in the terminal:

python Particulates.py > data.csv

I also tried:

python Particulates.py | data.csv

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.

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?

Thank you for your help.


回答1:


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)

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()] + data.data[:-2])
        except ReadTimeoutError:
            pms5003 = PMS5003()
except KeyboardInterrupt:
    pass


来源:https://stackoverflow.com/questions/61256199/piping-data-from-a-python-script-into-a-csv-or-txt-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!