Given the following Markdown table stored in
| 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
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
1,2,3,4,5 0,0,0,0,0 5,4,3,2,1 alice,bob,carl,dave,emil
🌍 Background Tutorials: The code uses a multitude of Python features. Check out these articles to learn more about them:
- Python slicing
- Python List Comprehension
- Python List
- Python List Concatenation
- Python Create DataFrame
- Python Pandas
- Working with Markdown Files in Python
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.