Advertisement
TakesxiSximada

create sql for space bottle

Apr 12th, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. #! /usr/bin/env python
  2. #-*- coding: utf-8 -*-
  3. import csv
  4. import optparse
  5.  
  6. template = """INSERT INTO [spacebottle].[Detail] (name, owner, operator, users, purpose, orbit_class, orbit_type, launch_mass, dry_mass, power, launch_date, lifetime, contractor, contractor_country, launch_site, launch_vehicle, cospar, norad,comment) VALUES({});"""
  7.  
  8. def main():
  9.     parser = optparse.OptionParser()
  10.     opts, args = parser.parse_args()
  11.  
  12.     filename = args[0]
  13.  
  14.     def safe_word(word):
  15.  
  16.         if "'" in word:
  17.             word = word.replace("'", "''")
  18.         return "'{}'".format(word)
  19.  
  20.     with open(filename, 'rb') as fp:
  21.         reader = csv.reader(fp)
  22.  
  23.         for ii, rows in enumerate(zip(*[iter(reader)]*150)):
  24.             with open('test.{}.txt'.format(ii), 'w+b') as bp:
  25.                 for row in rows:
  26.                     row = row[:19]
  27.                     row = map(safe_word, row)
  28.                     line = ','.join(row)
  29.                     line = template.format(line) + '\n'
  30.  
  31.                     bp.write(line)
  32.                     #print template.format(line) # outout to stdout
  33.  
  34.  
  35.  
  36. if __name__ == '__main__':
  37.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement