-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2json.py
More file actions
24 lines (18 loc) · 734 Bytes
/
Copy pathcsv2json.py
File metadata and controls
24 lines (18 loc) · 734 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import csv
import json
import sys
def csv_to_json(csv_file_path, json_file_path):
with open(csv_file_path, mode='r', newline='', encoding='utf-8') as csv_file:
reader = csv.DictReader(csv_file)
data = list(reader)
with open(json_file_path, mode='w', encoding='utf-8') as json_file:
json.dump(data, json_file, indent=2)
print(f"Converted '{csv_file_path}' to '{json_file_path}' successfully.")
# Example usage:
# python csv_to_json.py input.csv output.json
csv_to_json("tblproducts.csv", "products.json")
# if __name__ == "__main__":
# if len(sys.argv) != 3:
# print("Usage: python csv_to_json.py input.csv output.json")
# else:
# csv_to_json(sys.argv[1], sys.argv[2])