Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Convert Markdown Table to CSV

#1
Python Convert Markdown Table to CSV

5/5 – (1 vote)

Problem


Given the following Markdown table stored in 'my_file.md':

| 1 | 2 | 3 | 4 | 5 |
|-------|-----|------|------|------|
| 0 | 0 | 0 | 0 | 0 |
| 5 | 4 | 3 | 2 | 1 |
| alice | bob | carl | dave | emil |

? Python Challenge: How to convert the Markdown table to a CSV file 'my_file.csv'?


Solution


To convert a Markdown table .md file to a CSV file in Python, first read the Markdown table file by using the f.readlines() method on the opened file object f, by splitting along the markdown table separator symbol '|'. Clean up the resulting list (row-wise) and add all rows to a single list of lists. Then create a DataFrame from the list of lists and use the DataFrame.to_csv() method to write it to a CSV file.

An example is shown in the following script that you can use for your own conversion exercise by replacing only the in-file and out-file names highlighted below:

import pandas as pd # Convert the Markdown table to a list of lists
with open('my_file.md') as f: rows = [] for row in f.readlines(): # Get rid of leading and trailing '|' tmp = row[1:-2] # Split line and ignore column whitespace clean_line = [col.strip() for col in tmp.split('|')] # Append clean row data to rows variable rows.append(clean_line) # Get rid of syntactical sugar to indicate header (2nd row) rows = rows[:1] + rows[2:] print(rows)
df = pd.DataFrame(rows)
df.to_csv('my_file.csv', index=False, header=False)

The resulting CSV file 'my_file.csv':

1,2,3,4,5
0,0,0,0,0
5,4,3,2,1
alice,bob,carl,dave,emil

Learn More


? Background Tutorials: The code uses a multitude of Python features. Check out these articles to learn more about them:



https://www.sickgaming.net/blog/2022/08/...le-to-csv/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Convert MIDI to MP3 in Python – A Quick Overview xSicKxBot 0 2,394 09-02-2023, 02:04 PM
Last Post: xSicKxBot
  [Tut] How to Convert an Octal Escape Sequence in Python – And Vice Versa? xSicKxBot 0 1,331 12-08-2022, 01:23 PM
Last Post: xSicKxBot
  [Tut] How to Convert Octal String to Integer in Python xSicKxBot 0 1,297 12-04-2022, 08:39 AM
Last Post: xSicKxBot
  [Tut] Python Convert Hex to Base64 xSicKxBot 0 1,276 11-30-2022, 09:32 PM
Last Post: xSicKxBot
  [Tut] Working with Markdown Files in Python xSicKxBot 0 1,161 11-06-2022, 10:18 PM
Last Post: xSicKxBot
  [Tut] How to Convert Bool (True/False) to a String in Python? xSicKxBot 0 1,302 10-04-2022, 11:37 AM
Last Post: xSicKxBot
  [Tut] Python Convert Image (JPG, PNG) to CSV xSicKxBot 0 1,296 09-10-2022, 12:05 PM
Last Post: xSicKxBot
  [Tut] Python Convert Parquet to CSV xSicKxBot 0 1,183 09-02-2022, 03:20 PM
Last Post: xSicKxBot
  [Tut] How to Convert a Log to a CSV File in Python? xSicKxBot 0 1,290 08-30-2022, 02:11 AM
Last Post: xSicKxBot
  [Tut] Python – How to Convert KML to CSV? xSicKxBot 0 1,201 08-21-2022, 08:08 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016