Skip to content

Instantly share code, notes, and snippets.

@guyskk
Last active October 26, 2022 15:42
Show Gist options
  • Save guyskk/84d41f275d1510a1c127f64d55307e2c to your computer and use it in GitHub Desktop.
Save guyskk/84d41f275d1510a1c127f64d55307e2c to your computer and use it in GitHub Desktop.
import os
SQL = "INSERT INTO table (`value1`,`value2`,) VALUES ('%s','%s');\n"
def main():
for fname in os.listdir("."):
# step1
name, ext = os.path.splitext(fname)
if ext != ".txt":
continue
os.rename(fname, name + ".sql")
# step2
i = name.index("-")
left = name[:i]
# step3
with open(name + ".sql", "r") as f:
contents = f.read().split("\n")
with open(name + ".sql", "w") as f:
for value in contents:
f.write(SQL % (left, value))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment