Skip to content

Instantly share code, notes, and snippets.

@lll9p
Last active July 12, 2016 14:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lll9p/674cb25fe4546d87f1d865c415acacd5 to your computer and use it in GitHub Desktop.
import glob
import os
DIR = r'/XX'
SAVEDIR = r'./save'
txts = glob.glob(DIR + '/*.txt')
for txt in txts:
num = txt.split('-')[0]
s_list = []
with open(txt, mode='r') as f_old:
for s in f_old:
s_list.append(
"INSERT INTO table (`value1`,`value2`,) VALUES ('{:s}','{:s}');".format(s, num))
sql_new = os.path.join(SAVEDIR, os.path.splitext(os.path.split(txt)[1])[0]+'.sql')
with open(sql_new, mode='w') as f_new:
f_new.write('\n'.join(s_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment