Simple test
Ensure your device works with this simple test.
examples/csv_simpletest.py
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2# SPDX-FileCopyrightText: Copyright (c) 2021 Alec Delaney
3#
4# SPDX-License-Identifier: MIT
5
6"""Example file for working with CSV files one CIrcuitPython boards.
7
8This example assumes that an SD storage device is connected to the CircuitPython board.
9"""
10
11import board
12import sdcardio
13import storage
14
15import circuitpython_csv as csv
16
17# Initialize SD card
18spi = board.SPI()
19sdcard = sdcardio.SDCard(spi, board.D10)
20vfs = storage.VfsFat(sdcard)
21storage.mount(vfs, "/sd")
22
23# Write the CSV file!
24with open("/sd/testwrite.csv", mode="w", encoding="utf-8") as writablefile:
25 csvwriter = csv.writer(writablefile)
26 csvwriter.writerow(["I", "love", "CircuitPython", "!"])
27 csvwriter.writerow(["Spam"] * 3)
Disklogger
Logging data to .CSV file on CircuitPython Disk
examples/csv_disklogger.py
1# SPDX-FileCopyrightText: 2022 @Skicka for Adafruit Industries / Hakcat
2#
3# SPDX-License-Identifier: MIT
4
5
6"""Example file for logging data to .CSV file on CircuitPython disk.
7
8Note that this requires the CircuitPython board to be writable, which is not the
9default state for boards.
10
11If you get a read-only filesystem error, add "storage.remount('/', False)" in boot.py
12Make sure you add a way to reverse this in boot.py or your CP device won't show up via USB
13See example below:
14
15https://learn.adafruit.com/getting-started-with-raspberry-pi-pico-circuitpython/data-logger
16"""
17
18import os
19import random
20
21import circuitpython_csv as csv
22
23# Check if .CSV file is already present. If not, we write CSV headers.
24all_files = os.listdir() ## List all files in directory
25if "datelog.csv" not in all_files:
26 with open("datelog.csv", mode="w", encoding="utf-8") as writablefile:
27 csvwriter = csv.writer(writablefile)
28 csvwriter.writerow(["Year", "Month", "Day", "Hour", "Minute"])
29
30# Now that the file exists (or already did) we make a random date
31year = random.randint(1999, 2022)
32month = random.randint(1, 12)
33day = random.randint(1, 30)
34hour = random.randint(0, 23)
35minute = random.randint(0, 60)
36
37# We append this to the .CSV file
38with open("datelog.csv", mode="a", encoding="utf-8") as writablefile:
39 csvwriter = csv.writer(writablefile)
40 csvwriter.writerow([year, month, day, hour, minute])
41
42# Finally, we try to read back the last line in the CSV file to make sure it wrote.
43with open("datelog.csv", "r", encoding="utf-8") as file:
44 data = file.readlines()
45 print(data[-1])
DictWriter test
Illustrate an example of the DictWriter class
examples/csv_dictwritertest.py
1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2# SPDX-FileCopyrightText: Copyright (c) 2021 Alec Delaney
3#
4# SPDX-License-Identifier: MIT
5
6"""Example file for working with CSV files one CIrcuitPython boards, using DictWriter.
7
8This example assumes that an SD storage device is connected to the CircuitPython board.
9"""
10
11import board
12import sdcardio
13import storage
14
15import circuitpython_csv as csv
16
17# Initialize SD card
18spi = board.SPI()
19sdcard = sdcardio.SDCard(spi, board.D10)
20vfs = storage.VfsFat(sdcard)
21storage.mount(vfs, "/sd")
22
23header = ["name", "fav-board", "fav-wing"]
24
25my_info = {
26 "name": "Blinka",
27 "fav-board": "Feather M4 Express",
28 "fav-wing": "Adalogger FeatherWing",
29}
30
31with open("/sd/testwrite.csv", mode="w", encoding="utf-8") as writablefile:
32 csvwriter = csv.DictWriter(writablefile, header)
33 csvwriter.writeheader()
34 csvwriter.writerow(my_info)