-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
46 lines (39 loc) · 1.43 KB
/
main.py
File metadata and controls
46 lines (39 loc) · 1.43 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
import os
from dbfread import DBF
# Path to the folder containing .DBF files
dbf_folder = "pathtodbfs"
output_file = "dbf_schemas.txt"
# Function to map DBF field types to SQLite types
def map_field_type(field_type, field_length):
if field_type == "C":
return f"TEXT({field_length})"
elif field_type == "N":
return "INTEGER" if field_length <= 10 else "REAL"
elif field_type == "F":
return "REAL"
elif field_type == "D":
return "DATE"
elif field_type == "L":
return "BOOLEAN"
elif field_type == "M":
return "TEXT"
else:
return "TEXT"
# Open output file to write schemas
with open(output_file, "w") as f:
for file_name in os.listdir(dbf_folder):
if file_name.endswith(".DBF"):
file_path = os.path.join(dbf_folder, file_name)
table_name = os.path.splitext(file_name)[0]
# Read the DBF file
dbf_table = DBF(file_path, load=True)
# Start building the schema
schema = f"CREATE TABLE {table_name} (\n"
for field in dbf_table.fields:
field_name = field.name
field_type = map_field_type(field.type, field.length)
schema += f" {field_name} {field_type},\n"
schema = schema.rstrip(",\n") + "\n);\n\n"
# Write schema to the output file
f.write(schema)
print(f"Schemas extracted to {output_file}.")