SQL (10-Aug @ 18:25)

thedevel

Syntax Highlighted Code

  1. #!/usr/bin/env python
  2.  
  3. f = file(raw_input("Enter the path for the txt file: "))
  4. w = file(raw_input("Enter the path for the out SQL file: "),'w')
  5. [24 more lines...]

Plain Code

#!/usr/bin/env python

f = file(raw_input("Enter the path for the txt file: "))
w = file(raw_input("Enter the path for the out SQL file: "),'w')
w.write("INSERT INTO drug_listing (fda_seq_no,drug_firm,firm_code,strength,unit,rx_otc,tradename) VALUES\n")

# Initiate variables
tlist = []
tstring = ''

while (True):
    l = f.readline() # Read line in
    if not l: # If line is empty
        f.close()
        break
    llist = l.split() # Convert string into list
    if len(llist) is 5: # If strength and unit are missing
        llist.insert(3,'') # Insert blank
        llist.insert(4,'') # Insert blank
    x = '' # Empty string x
    x = x.join([i + ' ' for i in llist[6:]]).strip() # Join each list element in positions > 6
    nlist = llist[:6] # Add first 6 listings
    nlist.append(x) # Add tradename
    vtuple = tuple(nlist) # Covert list to tuple
    tlist.append(vtuple) # Add listing tuple to list
tstring = tstring.join([str(i) + ",\n" for i in tlist]) # Join each listing with a comma
w.write(str(tstring[:-2]) + ";") # Remove last newline and comma, add semicolon, write to file
w.close()