python write text file

How to write sensor data so seperate text file (Raspberry Pi, Python)

Ask QuestionAsked todayActive todayViewed 18 times0

I have a pm2.5 sensor attached to a raspberry pi. I got the code working for the sensor but I want to be able to store the values being outputted. I was wondering what I needed to change to be able to write the data to a text file.

Heres my code:

import time
import board
import busio
from digitalio import DigitalInOut, Direction, Pull
import adafruit_pm25

import serial
uart = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=0.25)

print("Found PM2.5 sensor, reading data...")

while True:

        aqdata = pm25.read()
        # print(aqdata)
    except RuntimeError:
        print("Unable to read from sensor, retrying...")

    print("Concentration Units (standard)")
        "PM 1.0: %d\tPM2.5: %d\tPM10: %d"
        % (aqdata["pm10 standard"], aqdata["pm25 standard"], aqdata["pm100 standard"])
    print("Concentration Units (environmental)")
        "PM 1.0: %d\tPM2.5: %d\tPM10: %d"
        % (aqdata["pm10 env"], aqdata["pm25 env"], aqdata["pm100 env"])
    print("Particles > 0.3um / 0.1L air:", aqdata["particles 03um"])
    print("Particles > 0.5um / 0.1L air:", aqdata["particles 05um"])
    print("Particles > 1.0um / 0.1L air:", aqdata["particles 10um"])
    print("Particles > 2.5um / 0.1L air:", aqdata["particles 25um"])
    print("Particles > 5.0um / 0.1L air:", aqdata["particles 50um"])
    print("Particles > 10 um / 0.1L air:", aqdata["particles 100um"])

pythonraspberry-pilorashareedit  follow  flag asked 8 hours agoJohn Hooft1 New contributor

  • Just as a minor note, try to use f-Strings if the Python version permits. Instead of doing your %d things, just do this: f"PM 1.0: {aqdata["pm10 env"]} ..." – Dustin 7 hours ago

add a comment

1 Answer


Use this:

measurement_values = [0.3, 0.5, 1.0]
measurement_strings = [f"Particles > {size}um / 0.1L air" for size in measurement_values]

with open("myfile.txt", "w") as f: 
    # Write block of string
    f.write("Block of string") 
    # Write whole list

shareedit  follow  flag answered 7 hours agoDustin11066 bronze badges

  • 1Did you mean to include the “f” in [f”Particles >….. – John Hooft 6 hours ago
  • 1Yes. This is called an f-string, where instead of using %d and format, you can simply write the variable names into your string in the {}. There are other letters you can put before strings, such as u (I believe unicode), r (raw, for instance if you don’t want the \ in filenames to mess up stuff) and likely a few more. – Dustin 6 hours ago

add a comment


Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.