Text to Table
Convert delimiter-separated text into HTML, Markdown, or CSV table format.
Frequently Asked Questions
Code Implementation
import csv
import io
def csv_to_markdown(csv_text: str) -> str:
reader = csv.reader(io.StringIO(csv_text.strip()))
rows = list(reader)
if not rows:
return ""
# Determine column widths
col_count = max(len(row) for row in rows)
widths = [0] * col_count
for row in rows:
for i, cell in enumerate(row):
widths[i] = max(widths[i], len(cell))
def fmt_row(row):
padded = [cell.ljust(widths[i]) for i, cell in enumerate(row)]
# Pad missing columns
while len(padded) < col_count:
padded.append(" " * widths[len(padded)])
return "| " + " | ".join(padded) + " |"
lines = []
lines.append(fmt_row(rows[0]))
separator = "| " + " | ".join("-" * w for w in widths) + " |"
lines.append(separator)
for row in rows[1:]:
lines.append(fmt_row(row))
return "\n".join(lines)
csv_data = """Name,Age,City
Alice,30,New York
Bob,25,London"""
print(csv_to_markdown(csv_data))Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.