cs-3431-assignment-1/makeinserts.py

33 lines
883 B
Python
Raw Permalink Normal View History

import glob
import os.path
def is_num(val):
return val.replace('.', '', 1).isdigit()
def process_file(filename, f):
table_name = os.path.basename(filename[:-4] if filename.endswith('.csv') else filename)
print(f'-- {table_name} values')
for line in f:
stripped_line = line.strip()
contents = stripped_line.split(',')
query_items = []
for val in contents:
if not is_num(val) and val[0] != "'" and val[-1] != "'":
query_items.append(f"'{val}'")
else:
query_items.append(val)
value_string = ', '.join(query_items)
print(f'INSERT INTO {table_name} VALUES ({value_string});')
if __name__ == '__main__':
files = glob.glob('data/*.csv')
for filename in files:
with open(filename) as f:
process_file(filename, f)
print("")