-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv-to-json.py
More file actions
95 lines (79 loc) · 3.2 KB
/
csv-to-json.py
File metadata and controls
95 lines (79 loc) · 3.2 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
91
92
93
94
95
#!/usr/bin/env python3
"""
CSV to JSON Converter — Convert CSV files to JSON format.
Usage:
python csv-to-json.py <input.csv> [--output output.json] [--pretty]
python csv-to-json.py <input.csv> --delimiter ";"
Options:
--output FILE Output JSON file (default: <input>.json)
--pretty Pretty-print JSON with indentation
--delimiter CHAR CSV delimiter (default: ",")
--encoding ENC File encoding (default: utf-8)
--help Show this help message and exit
"""
import os
import sys
import csv
import json
import argparse
from pathlib import Path
def csv_to_json(input_path: Path, delimiter: str, encoding: str) -> list[dict]:
"""Read a CSV file and return a list of dicts."""
rows: list[dict] = []
try:
with open(input_path, "r", encoding=encoding, newline="") as f:
reader = csv.DictReader(f, delimiter=delimiter)
if reader.fieldnames is None:
print(f"Error: '{input_path}' appears empty or has no header row.")
sys.exit(1)
for row in reader:
# Strip whitespace from keys and values
cleaned = {k.strip() if k else k: v.strip() if isinstance(v, str) else v
for k, v in row.items()}
rows.append(cleaned)
except FileNotFoundError:
print(f"Error: File '{input_path}' not found.")
sys.exit(1)
except UnicodeDecodeError:
print(f"Error: Could not decode '{input_path}' with encoding '{encoding}'. "
f"Try --encoding utf-8-sig or latin-1.")
sys.exit(1)
except Exception as e:
print(f"Error reading CSV: {e}")
sys.exit(1)
return rows
def main():
parser = argparse.ArgumentParser(
description="Convert a CSV file to JSON.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=(
"Examples:\n"
" python csv-to-json.py data.csv\n"
" python csv-to-json.py data.csv --pretty\n"
" python csv-to-json.py data.tsv --delimiter $'\\t'\n"
),
)
parser.add_argument("input", help="Input CSV file")
parser.add_argument("--output", help="Output JSON file (default: <input>.json)")
parser.add_argument("--pretty", action="store_true", help="Pretty-print JSON")
parser.add_argument("--delimiter", default=",", help="CSV delimiter (default: ',')")
parser.add_argument("--encoding", default="utf-8", help="File encoding (default: utf-8)")
args = parser.parse_args()
input_path = Path(args.input)
if not input_path.is_file():
print(f"Error: '{args.input}' is not a valid file.")
sys.exit(1)
output_path = Path(args.output) if args.output else input_path.with_suffix(".json")
rows = csv_to_json(input_path, args.delimiter, args.encoding)
json_kwargs: dict = {"ensure_ascii": False}
if args.pretty:
json_kwargs["indent"] = 2
try:
with open(output_path, "w", encoding="utf-8") as f:
json.dump(rows, f, **json_kwargs)
except Exception as e:
print(f"Error writing JSON: {e}")
sys.exit(1)
print(f"Converted {len(rows)} rows → {output_path}")
if __name__ == "__main__":
main()