PicoSeries PR

How to read and write csv on RaspberryPi Pico series ~ MicroPython ~

記事内に商品プロモーションを含む場合があります

Introduction

In this blog post, I will explain how to use CSV files in MicroPython for the Raspberry Pi Pico series.

When using the Pico series, you may sometimes think, “I want to output this data in CSV format.

MicroPython’s firmware allows you to use the virtual file system and Python’s powerful string processing, so you can read and write CSVs just like you would on a PC, including skip headers and comma delimiters.

It is just the right file format for a little input/output. Please take a look if you are interested.

Not necessarily pico2

The Pico2 is used in this blog post, but the exact same wiring and programme can be used with the Pico and Pico W.
If you don’t have a Pico2 yet, please try it out with your Pico series.

Environment

The environment used in this blog post is the following.

environment Version etc. Comments
Developing PC Windows11 Windows 10’s fine.
Language MicroPython  Ver.1.24.0
Development Environment Thonny  Ver.4.1.4
board RaspberryPi Pico2 Can also use Pico, PicoW

Connection with RaspberryPi Pico series

The Pico series can be used by itself. The Pico series can be used by itself.

Parts in use

Ready-to-use kits are also available.

Ready to use kits with soldered headers, USB cable and pin layout chart are also available.

Program overview

The program outline for this blog post is as follows.

  • Writing CSV files
  • Reading CSV Files
  • Skipping headers
  • Splitting by comma

Results of running the program

The following is the result of running the program described below.

After the CSV file is created, its contents are displayed in the terminal part of Thonny.

CSVファイルの読み書きをするプログラムの実行結果を説明する画像
Result

If the file does not appear after running the program, try clicking “Refresh” from the settings.

CSVファイルが表示されない場合の対応を説明する画像

If CSV file is not displayed

Entire code

The full text of the Code is as follows. The details are explained in the section “Key Points of the Code” below.

import os

# Specify a filename
fileName = "pico_spec.csv"

# Open the file in write mode
f = open( fileName,  'w')

# Write content
f.write("Name,Price,Wireless\n")
f.write("Pico,$4,No\n")
f.write("Pico W,$7,Yes\n")
f.write("Pico 2,$5,No\n")

# Closes the file when writing is complete. 
f.close()

# Reopen the file in read mode 
f = open(fileName, "r")

# Skip headers.
next(f)

# Reads lines one by one and displays them separated by commas
for line in f:
    words = line.split(",")
    print( "Name: " + words[0] )
    print( "Price: " + words )
    print( "Wireless: " + words )

# Closes the file when writing is complete.
f.close()

To run the program with the Pico series alone (without Thonny), save the program as “main.py” in the Pico series main unit.

Key Points of the Code

Writing CSV files

In the openfunction, specify “w” for write and a file name to open the file in write mode (or create a new file if no file exists).

After opening, use the writefunction to enter headers (information such as item names) on the first line, separated by commas.
On the next line, enter the contents of the items, separated by commas.

When you have finished writing, close the file using the closefunction.

# Open the file in write mode
f = open( fileName,  'w')

# Write content
f.write("Name,Price,Wireless\n")
f.write("Pico,$4,No\n")
f.write("Pico W,$7,Yes\n")
f.write("Pico 2,$5,No\n")

# Closes the file when writing is complete. 
f.close()

We recommend that you do not include spaces to make the elements easier to read (alignment); MicroPython does not support the replace function, so it will be difficult to remove spaces before and after reading.

Reading CSV Files

As with writing, specify “r” to read and a filename in the open function to open the file.

# Reopen the file in read mode 
f = open(fileName, "r")

Skipping headers

After the file is opened, it is read line by line, but the first line is the header (item name), which can get in the way if you want to use the data part of the file.

Therefore, the nextfunction is used to skip reading the first line.

# Skip headers.
next(f)

Split by comma

After skipping the header, the data rows are read one by one in a for loop.
Specify a comma in thesplitfunction to split each element into a list.

Once the above is done, each element of the CSV can be accessed by index. (In this case, print to view each element)

# Reads lines one by one and displays them separated by commas
for line in f:
    words = line.split(",")
    print( "Name: " + words[0] )
    print( "Price: " + words )
    print( "Wireless: " + words )

# Closes the file when writing is complete.
f.close()

Finally

In this blog post, I explained how to use CSV files with MicroPython in the Raspberry Pi Pico series.

JSON and XML are also convenient, but CSV files are more convenient for quick input/output.

With Pico series + MicroPython, you can read and write CSV files just as easily as when using Python on a PC.

Knowing this makes it easier to improve and debug, so if you are interested, please give it a try.

I would be happy to be of help.

質問・要望 大歓迎です

「こんな解説記事作って」「こんなことがしたいけど、〇〇で困ってる」など、コメント欄で教えてください。 質問・要望に、中の人ができる限り対応します。

使えたよ・設定できたよの一言コメントも大歓迎。気軽に足跡を残してみてください。記事を紹介したい方はブログ、SNSにバシバシ貼ってもらってOKです。

ABOUT ME
えす
現役のソフトウェアエンジニアです。 C++ C# Python を使ってます。10年ちょい設計/開発部門にいましたが、今はQAエンジニアっぽいことをしています。

COMMENT

Your email address will not be published. Required fields are marked *

Index