-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.py
32 lines (25 loc) · 928 Bytes
/
make.py
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
import argparse
import csv
CSV_FILE = "iranAdministrativeDivision.csv"
SQL_FILE = "iranAdministrativeDivision.sql"
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('table', type=str)
args = parser.parse_args()
table = args.table
def readCsv():
with open(CSV_FILE, 'r') as f:
csvreader = csv.reader(f)
header = next(csvreader)
for raw in csvreader:
row = {}
for i in range(len(header)):
row[header[i]] = raw[i]
yield row
with open(SQL_FILE, 'w') as f:
f.write("SET NAMES 'utf8mb4' COLLATE 'utf8mb4_persian_ci';\n")
f.write(f"TRUNCATE TABLE {table};\n")
for row in readCsv():
if not row['parent']:
row['parent'] = 'null'
f.write(f"INSERT INTO {table}(`id`, `title`, `type`, `parent`) " +
f"VALUES({row['id']}, '{row['title']}', '{row['type']}', {row['parent']});\n")