はじめに

CSVをMarkdownに変換する csvtomdというコマンドがあるということで使用してみる。

環境

1
2
MacOS Sonoma 14.0
Homebrew 4.1.19

csvtomdのインストール

1
$ brew install csvtomd
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$ brew install csvtomd
Running `brew update --auto-update`...
==> Downloading https://ghcr.io/v2/homebrew/core/csvtomd/manifests/0.3.0_3-2
################################################################################################################################ 100.0%
==> Fetching csvtomd
==> Downloading https://ghcr.io/v2/homebrew/core/csvtomd/blobs/sha256:bd2866f71ebb4f2f53351e55813fb68acd6daa645a27f9d7f1878debca3f877d
################################################################################################################################ 100.0%
==> Pouring csvtomd--0.3.0_3.sonoma.bottle.2.tar.gz
🍺  /usr/local/Cellar/csvtomd/0.3.0_3: 19 files, 23.8KB
==> Running `brew cleanup csvtomd`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

使ってみる

1
csvtomd --help
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$ csvtomd --help
usage: csvtomd [-h] [-n] [-p PADDING] [-d DELIMITER] [csv_file ...]

Read one or more CSV files and output their contents in the form of Markdown tables.

positional arguments:
  csv_file              One or more CSV files to be converted. Use - for stdin.

options:
  -h, --help            show this help message and exit
  -n, --no-filenames    Don't display filenames when outputting multiple Markdown tables.
  -p PADDING, --padding PADDING
                        The number of spaces to add between table cells and column dividers. Default is 2 spaces.
  -d DELIMITER, --delimiter DELIMITER
                        The delimiter to use when parsing CSV data. Default is ","

こんな感じになる。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
~/Downloads$csvtomd c01_utf-8.csv 
都道府県コード  |  都道府県名        |  元号  |  和暦(年)  |  西暦(年)  |  注   |  人口(総数)     |  人口(男)     |  人口(女)
---------|---------------|------|---------|---------|------|-------------|------------|----------
00       |  全国           |  大正  |  9      |  1920   |      |  55963053   |  28044185  |  27918868
01       |  北海道          |  大正  |  9      |  1920   |      |  2359183    |  1244322   |  1114861
02       |  青森県          |  大正  |  9      |  1920   |      |  756454     |  381293    |  375161
03       |  岩手県          |  大正  |  9      |  1920   |      |  845540     |  421069    |  424471
04       |  宮城県          |  大正  |  9      |  1920   |      |  961768     |  485309    |  476459
05       |  秋田県          |  大正  |  9      |  1920   |      |  898537     |  453682    |  444855
06       |  山形県          |  大正  |  9      |  1920   |      |  968925     |  478328    |  490597
07       |  福島県          |  大正  |  9      |  1920   |      |  1362750    |  673525    |  689225
08       |  茨城県          |  大正  |  9      |  1920   |      |  1350400    |  662128    |  688272
09       |  栃木県          |  大正  |  9      |  1920   |      |  1046479    |  514255    |  532224
10       |  群馬県          |  大正  |  9      |  1920   |      |  1052610    |  514106    |  538504

Markdownになるので、実際に表示させてみる。

都道府県コード都道府県名元号和暦(年)西暦(年)人口(総数)人口(男)人口(女)
00全国大正91920559630532804418527918868
01北海道大正91920235918312443221114861
02青森県大正91920756454381293375161
03岩手県大正91920845540421069424471
04宮城県大正91920961768485309476459
05秋田県大正91920898537453682444855
06山形県大正91920968925478328490597
07福島県大正919201362750673525689225
08茨城県大正919201350400662128688272
09栃木県大正919201046479514255532224
10群馬県大正919201052610514106538504

おわりに

csvをマークダウンに変換するwebサイト(https://markdown-convert.com/ja/tool/table)やVSCodeの拡張(https://takezoe.hatenablog.com/entry/2021/01/09/213320)などがあるので、方法としてはたくさんある中で今回はコマンドでの方法を実践してみた。
コマンドで入れていることを忘れて、webサイトかvscode拡張の方を使いそう・・・