Convertire un file CSV in una tabella MD in Python

Markdown
Markdown

Utilizzo spesso Markdown perché è un modo veloce di produrre documenti formattati che ospitano molto codice e contenuti tabulari. Con Python, qui di seguito descrivo un modo per convertire una tabella csv in una tabella markdown:

Step 1. Intallazione di Pandas

$ pip install pandas
Defaulting to user installation because normal site-packages is not writeable
Collecting pandas
  Downloading pandas-2.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.3/12.3 MB 5.2 MB/s eta 0:00:00
Requirement already satisfied: pytz>=2020.1 in /usr/lib/python3/dist-packages (from pandas) (2022.1)
Requirement already satisfied: numpy>=1.21.0 in /usr/lib/python3/dist-packages (from pandas) (1.21.5)
Collecting tzdata>=2022.1
  Downloading tzdata-2023.3-py2.py3-none-any.whl (341 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 341.8/341.8 kB 4.0 MB/s eta 0:00:00
Collecting python-dateutil>=2.8.2
  Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 247.7/247.7 kB 4.8 MB/s eta 0:00:00
Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)
Installing collected packages: tzdata, python-dateutil, pandas
Successfully installed pandas-2.0.0 python-dateutil-2.8.2 tzdata-2023.3

Step 2. Installazione di Tabulate

$ pip install tabulate
Defaulting to user installation because normal site-packages is not writeable
Collecting tabulate
  Downloading tabulate-0.9.0-py3-none-any.whl (35 kB)
Installing collected packages: tabulate
Successfully installed tabulate-0.9.0

Step 3. Programma Python

import pandas as pd

df = pd.read_csv("source.csv")
with open("destination.md", 'w') as md:
  df.to_markdown(buf=md, tablefmt="grid")

Questo è l’output prodotto su un file CSV di prova:

|    |      ID |   CODE |   CODE2 |   QUANTITY |   ASPECT |   CATEGORY |   BULK_ID |   ITEM_ID |   SHARE_ID | STATUS   | LOCK   |
|----|---------|-----------------------------|------------------|------------------|------------|----------------|---------------|-----------|-------------|-----------------|-------------|
|  0 | 1033896 |                      350821 |              446 |                7 |          3 |              5 |      40090302 |         4 |       21278 | S               | N           |
|  1 | 1033617 |                      350797 |              446 |               10 |          4 |              5 |      40090302 |         4 |       21278 | S               | N           |
|  2 | 1033903 |                      350822 |              446 |              107 |          3 |              5 |      40090302 |         4 |       21278 | S               | N           |
|  3 | 1033935 |                      350823 |              446 |              108 |          3 |              5 |      40090302 |         4 |       21278 | S               | N           |

che renderizzato in ReText (editor MD) si vede così:

Riferimenti web

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.