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
 6import board
 7import sdcardio
 8import storage
 9import circuitpython_csv as csv
10
11# Initialize SD card
12spi = board.SPI()
13sdcard = sdcardio.SDCard(spi, board.D10)
14vfs = storage.VfsFat(sdcard)
15storage.mount(vfs, "/sd")
16
17# Write the CSV file!
18with open("/sd/testwrite.csv", mode="w", encoding="utf-8") as writablefile:
19    csvwriter = csv.writer(writablefile)
20    csvwriter.writerow(["I", "love", "CircuitPython", "!"])
21    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# Logging data to .CSV file on CircuitPython Disk
 3# SPDX-License-Identifier: MIT
 4
 5# If you get a read-only filesystem error, add "storage.remount('/', False)" in boot.py
 6# Make sure you add a way to reverse this in boot.py or your CP device won't show up via USB
 7# See example below:
 8# https://learn.adafruit.com/getting-started-with-raspberry-pi-pico-circuitpython/data-logger
 9
10import os
11import random
12import circuitpython_csv as csv
13
14# Check if .CSV file is already present. If not, we write CSV headers.
15all_files = os.listdir()  ## List all files in directory
16if "datelog.csv" not in all_files:
17    with open("datelog.csv", mode="w", encoding="utf-8") as writablefile:
18        csvwriter = csv.writer(writablefile)
19        csvwriter.writerow(["Year", "Month", "Day", "Hour", "Minute"])
20
21# Now that the file exists (or already did) we make a random date
22year = random.randint(1999, 2022)
23month = random.randint(1, 12)
24day = random.randint(1, 30)
25hour = random.randint(0, 23)
26minute = random.randint(0, 60)
27
28# We append this to the .CSV file
29with open("datelog.csv", mode="a", encoding="utf-8") as writablefile:
30    csvwriter = csv.writer(writablefile)
31    csvwriter.writerow([year, month, day, hour, minute])
32
33# Finally, we try to read back the last line in the CSV file to make sure it wrote.
34with open("datelog.csv", "r", encoding="utf-8") as file:
35    data = file.readlines()
36    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
 6import board
 7import sdcardio
 8import storage
 9import circuitpython_csv as csv
10
11# Initialize SD card
12spi = board.SPI()
13sdcard = sdcardio.SDCard(spi, board.D10)
14vfs = storage.VfsFat(sdcard)
15storage.mount(vfs, "/sd")
16
17header = ["name", "fav-board", "fav-wing"]
18
19my_info = {
20    "name": "Blinka",
21    "fav-board": "Feather M4 Express",
22    "fav-wing": "Adalogger FeatherWing",
23}
24
25with open("/sd/testwrite.csv", mode="w", encoding="utf-8") as writablefile:
26    csvwriter = csv.DictWriter(writablefile, header)
27    csvwriter.writeheader()
28    csvwriter.writerow(my_info)