-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcsv2md.py
More file actions
executable file
·90 lines (70 loc) · 2.59 KB
/
Copy pathcsv2md.py
File metadata and controls
executable file
·90 lines (70 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python3
# Tool to convert arbitrary CSV to Markdown
import csv
import os
import sys
from argparse import ArgumentParser
def try_utf8(filehdl):
"Returns a Unicode object on success, or None on failure"
try:
data = filehdl.read()
except UnicodeDecodeError:
return None
if not data:
return None
try:
return data.decode('utf-8')
except UnicodeDecodeError:
return None
def blank_md(md_file,message):
# CSV is empty, write message to Markdown file
with open(md_file, 'w') as f:
f.write(message)
exit()
def csv_to_markdown(csv_file, md_file):
with open(csv_file, 'rb') as f:
utfdata = try_utf8(f)
if utfdata is None:
print("Error: CSV file is not UTF-8 encoded")
blank_md(md_file,"Output of parser is not UTF-8 encoded")
# the file should be fine, let's read it again
with open(csv_file, 'r') as f:
reader = csv.DictReader(f)
rows = list(reader)
if not rows:
print("NOTE: CSV file is empty")
blank_md(md_file,"No data.")
# All exceptions should be handled, lets continue
headers = [key for key in rows[0].keys()]
md_table = f'| {" | ".join(headers)} |\n'
md_table += f'| {" | ".join(["---"]*len(headers))} |\n'
for row in rows:
md_table += f'| {" | ".join(str(x) for x in row.values())} |\n'
with open(md_file, 'w') as f:
f.write(md_table)
if __name__ == '__main__':
parser = ArgumentParser(description='Convert CSV file to Markdown format')
parser.add_argument('input_file',
help='Input CSV file path')
parser.add_argument('-o', '--output',
help='Output Markdown file path (default: input_file_name.md)')
args = parser.parse_args()
# Check if input file exists
if not os.path.exists(args.input_file):
print(f"Error: Input file '{args.input_file}' does not exist", file=sys.stderr)
sys.exit(1)
# Check if input file is a CSV file
if not args.input_file.lower().endswith('.csv'):
print(f"Warning: Input file '{args.input_file}' does not have .csv extension",
file=sys.stderr)
# Determine output file path
if args.output:
md_file = args.output
else:
# Default: replace .csv with .md in input filename
md_file = os.path.splitext(args.input_file)[0] + '.md'
try:
csv_to_markdown(args.input_file, md_file)
except Exception as e:
print(f"Error during conversion: {str(e)}", file=sys.stderr)
sys.exit(1)