fork download
  1. import sys
  2.  
  3. def pad(markdown):
  4. cells = [[cell.strip() for cell in line.split('|')] for line in markdown.splitlines()]
  5. if all(all(x == '-' for x in cell) for cell in cells[1]):
  6. del cells[1]
  7. width = [max(len(item) for item in items) for items in zip(*cells)]
  8. header = False
  9. for line in cells:
  10. formatted = []
  11. for idx, col in enumerate(line):
  12. formatted.append(f' %{-width[idx]-1}s' % line[idx])
  13. print('|'.join(formatted).strip())
  14. if not header:
  15. print('|'.join([' ' + ''.ljust(w, '-') + ' ' for w in width]).strip())
  16. header = True
  17.  
  18. pad(sys.stdin.read())
Success #stdin #stdout 0.02s 9368KB
stdin
| Col1     | Col2 |   Col3 |
| ---- | ---- | --------- |
| Val1| Val2| Val3 |
stdout
| Col1 | Col2 | Col3 |
| ---- | ---- | ---- |
| Val1 | Val2 | Val3 |