forked from Azure-Samples/rag-postgres-openai-python
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconvert_csv_json.py
More file actions
51 lines (46 loc) · 1.91 KB
/
convert_csv_json.py
File metadata and controls
51 lines (46 loc) · 1.91 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
import ast
import csv
import json
# Read CSV file - Using the correct dialect to handle quotes properly
with open("data.csv", encoding="utf-8") as csv_file:
# Use the csv.reader with proper quoting parameters
csv_reader = csv.reader(csv_file, quoting=csv.QUOTE_ALL, doublequote=True, escapechar="\\")
header = next(csv_reader) # Get the header row
data = list(csv_reader) # Get all data rows
# Convert to JSON format
json_data = []
for row in data:
item = {}
for i in range(len(header)):
if i < len(row): # Ensure we don't go out of bounds
value = row[i].strip()
# Check if the value looks like a JSON array
if value.startswith("[") and value.endswith("]"):
try:
# Parse the JSON-like string into a Python object
value = json.loads(value.replace("'", '"'))
except (ValueError, SyntaxError):
try:
# Try with ast as a fallback
value = ast.literal_eval(value)
except (ValueError, SyntaxError):
# If parsing fails, keep it as a string
pass
# Convert boolean strings
elif value.lower() == "true":
value = True
elif value.lower() == "false":
value = False
# Try to convert numbers
elif value.isdigit():
value = int(value)
elif value.replace(".", "", 1).isdigit() and value.count(".") <= 1:
value = float(value)
item[header[i]] = value
# remove is_open column
del item["is_open"]
json_data.append(item)
# Write to JSON file
with open("data.json", "w", encoding="utf-8") as f:
json.dump(json_data, f, indent=4, ensure_ascii=False)
print(f"Successfully converted CSV data to JSON format with {len(json_data)} records")