09-01-2022, 01:21 AM
Python Convert Markdown Table to CSV
<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"626351","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2>Problem</h2>
<p>Given the following Markdown table stored in <code>'my_file.md'</code>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="md" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">| 1 | 2 | 3 | 4 | 5 |
|-------|-----|------|------|------|
| 0 | 0 | 0 | 0 | 0 |
| 5 | 4 | 3 | 2 | 1 |
| alice | bob | carl | dave | emil |</pre>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f40d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Python Challenge</strong>: How to convert the Markdown table to a CSV file <code>'my_file.csv'</code>?</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="585" height="268" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-97.png" alt="" class="wp-image-626397" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-97.png 585w, https://blog.finxter.com/wp-content/uplo...00x137.png 300w" sizes="(max-width: 585px) 100vw, 585px" /></figure>
</div>
<h2>Solution</h2>
<p class="has-base-background-color has-background">To convert a Markdown table <code>.md</code> file to a CSV file in Python, first read the Markdown table file by using the <code>f.readlines()</code> method on the <a href="https://blog.finxter.com/python-open-function/" data-type="post" data-id="24793" target="_blank" rel="noreferrer noopener">opened</a> file object <code>f</code>, by <a href="https://blog.finxter.com/python-string-split/" data-type="post" data-id="26097" target="_blank" rel="noreferrer noopener">splitting</a> along the markdown table separator symbol <code>'|'</code>. Clean up the resulting list (row-wise) and add all rows to a single <a href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890" target="_blank" rel="noreferrer noopener">list of lists</a>. Then create a DataFrame from the list of lists and use the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-dataframe-to_csv-method/" data-type="post" data-id="344277" target="_blank">DataFrame.to_csv()</a></code> method to write it to a CSV file.</p>
<p>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:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4, 23" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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) </pre>
<p>The resulting CSV file <code>'my_file.csv'</code>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1,2,3,4,5
0,0,0,0,0
5,4,3,2,1
alice,bob,carl,dave,emil
</pre>
<h2>Learn More</h2>
<p><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Background Tutorials</strong>: The code uses a multitude of Python features. Check out these articles to learn more about them:</p>
<ul class="has-base-background-color has-background">
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-open-function/" data-type="post" data-id="24793" target="_blank">Python <code>open()</code> function</a></li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="post" data-id="731" target="_blank">Python slicing</a></li>
<li><a href="https://blog.finxter.com/list-comprehension/" data-type="post" data-id="1171" target="_blank" rel="noreferrer noopener">Python List Comprehension</a></li>
<li><a href="https://blog.finxter.com/python-list-append/" data-type="post" data-id="6605" target="_blank" rel="noreferrer noopener">Python List <code>append()</code> Method</a></li>
<li><a href="https://blog.finxter.com/python-list-concatenation-add-vs-inplace-add-vs-extend/" data-type="post" data-id="8676" target="_blank" rel="noreferrer noopener">Python List Concatenation</a></li>
<li><a href="https://blog.finxter.com/how-to-create-a-dataframe-in-pandas/" data-type="post" data-id="16764" target="_blank" rel="noreferrer noopener">Python Create DataFrame</a></li>
<li><a href="https://blog.finxter.com/pandas-dataframe-to_csv-method/" data-type="post" data-id="344277" target="_blank" rel="noreferrer noopener">Python Pandas <code>to_csv()</code> Method</a></li>
</ul>
</div>
https://www.sickgaming.net/blog/2022/08/...le-to-csv/
<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{"align":"left","id":"626351","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2>Problem</h2>
<p>Given the following Markdown table stored in <code>'my_file.md'</code>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="md" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">| 1 | 2 | 3 | 4 | 5 |
|-------|-----|------|------|------|
| 0 | 0 | 0 | 0 | 0 |
| 5 | 4 | 3 | 2 | 1 |
| alice | bob | carl | dave | emil |</pre>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f40d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Python Challenge</strong>: How to convert the Markdown table to a CSV file <code>'my_file.csv'</code>?</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="585" height="268" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-97.png" alt="" class="wp-image-626397" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-97.png 585w, https://blog.finxter.com/wp-content/uplo...00x137.png 300w" sizes="(max-width: 585px) 100vw, 585px" /></figure>
</div>
<h2>Solution</h2>
<p class="has-base-background-color has-background">To convert a Markdown table <code>.md</code> file to a CSV file in Python, first read the Markdown table file by using the <code>f.readlines()</code> method on the <a href="https://blog.finxter.com/python-open-function/" data-type="post" data-id="24793" target="_blank" rel="noreferrer noopener">opened</a> file object <code>f</code>, by <a href="https://blog.finxter.com/python-string-split/" data-type="post" data-id="26097" target="_blank" rel="noreferrer noopener">splitting</a> along the markdown table separator symbol <code>'|'</code>. Clean up the resulting list (row-wise) and add all rows to a single <a href="https://blog.finxter.com/python-list-of-lists/" data-type="post" data-id="7890" target="_blank" rel="noreferrer noopener">list of lists</a>. Then create a DataFrame from the list of lists and use the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-dataframe-to_csv-method/" data-type="post" data-id="344277" target="_blank">DataFrame.to_csv()</a></code> method to write it to a CSV file.</p>
<p>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:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4, 23" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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) </pre>
<p>The resulting CSV file <code>'my_file.csv'</code>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">1,2,3,4,5
0,0,0,0,0
5,4,3,2,1
alice,bob,carl,dave,emil
</pre>
<h2>Learn More</h2>
<p><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Background Tutorials</strong>: The code uses a multitude of Python features. Check out these articles to learn more about them:</p>
<ul class="has-base-background-color has-background">
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-open-function/" data-type="post" data-id="24793" target="_blank">Python <code>open()</code> function</a></li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="post" data-id="731" target="_blank">Python slicing</a></li>
<li><a href="https://blog.finxter.com/list-comprehension/" data-type="post" data-id="1171" target="_blank" rel="noreferrer noopener">Python List Comprehension</a></li>
<li><a href="https://blog.finxter.com/python-list-append/" data-type="post" data-id="6605" target="_blank" rel="noreferrer noopener">Python List <code>append()</code> Method</a></li>
<li><a href="https://blog.finxter.com/python-list-concatenation-add-vs-inplace-add-vs-extend/" data-type="post" data-id="8676" target="_blank" rel="noreferrer noopener">Python List Concatenation</a></li>
<li><a href="https://blog.finxter.com/how-to-create-a-dataframe-in-pandas/" data-type="post" data-id="16764" target="_blank" rel="noreferrer noopener">Python Create DataFrame</a></li>
<li><a href="https://blog.finxter.com/pandas-dataframe-to_csv-method/" data-type="post" data-id="344277" target="_blank" rel="noreferrer noopener">Python Pandas <code>to_csv()</code> Method</a></li>
</ul>
</div>
https://www.sickgaming.net/blog/2022/08/...le-to-csv/