25 lines
903 B
Python
25 lines
903 B
Python
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import sys
|
|
|
|
INPUT_DIRECTORY = sys.argv[1]
|
|
|
|
files_to_read = [x for x in os.listdir(INPUT_DIRECTORY) if not os.path.isdir(os.path.join(INPUT_DIRECTORY, x))]
|
|
|
|
for filename in files_to_read:
|
|
db_name = filename[:-4] if filename.lower().endswith(".csv") else filename
|
|
values = []
|
|
with open(os.path.join(INPUT_DIRECTORY, filename)) as f:
|
|
for line in f.readlines()[1:]:
|
|
line = line.strip()
|
|
parts = list(map(lambda x: x.strip(), line.split(",")))
|
|
for i, part in enumerate(parts):
|
|
if not (part.isnumeric() or (part.startswith("'") and part.endswith("'"))):
|
|
part = f"'{part}'"
|
|
parts[i] = part
|
|
|
|
values.append(", ".join(parts))
|
|
|
|
values = map(lambda x: f"({x.strip()})", values)
|
|
print(f"INSERT INTO {db_name} VALUES {', '.join(values)};")
|