Reputation: 18
Thank you for your consideration.
When I transfer talbe from excel to markdown I use pandas library.
Str from pd.to_markdown alwasy applied alignment.
But I want to remove alignment in markdown table when I make str by pd.to_markdown.
here are some example.
post_it width height
653 51 38
654 76 76
656 76 51
657 102 76
import pandas as pd
df=pd.read_clipboard()
markdown_table=df.to_markdown(index=False)
print(df)
post_it width height
0 653 51 38
1 654 76 76
2 656 76 51
3 657 102 76
print(markdown_table)
| post_it | width | height |
|----------:|--------:|---------:|
| 653 | 51 | 38 |
| 654 | 76 | 76 |
| 656 | 76 | 51 |
| 657 | 102 | 76 |
| post_it | width | height |
|-----------|---------|----------|
| 653 | 51 | 38 |
| 654 | 76 | 76 |
| 656 | 76 | 51 |
| 657 | 102 | 76 |
Upvotes: 0
Views: 1427
Reputation: 471
The to_markdown()
method has a tablefmt
parameter.
This looks like what you want:
print(df.to_markdown(tablefmt="github", index=False))
# Output
| post_it | width | height |
|-----------|---------|----------|
| 653 | 51 | 38 |
| 654 | 76 | 76 |
| 656 | 76 | 51 |
| 657 | 102 | 76 |
There are many formats to choose from if this isn't what you want. They're listed in the docs: https://github.com/astanin/python-tabulate
Upvotes: 3