Tip: Click lines to highlight, hold ctrl/cmd to multi-select

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. w.write("INSERT INTO drug_listing (fda_seq_no,drug_firm,firm_code,strength,unit,rx_otc,tradename) VALUES\n")
  6.  
  7. # Initiate variables
  8. tlist = []
  9. tstring = ''
  10.  
  11. while (True):
  12.     l = f.readline() # Read line in
  13.     if not l: # If line is empty
  14.         f.close()
  15.         break
  16.     llist = l.split() # Convert string into list
  17.     if len(llist) is 5: # If strength and unit are missing
  18.         llist.insert(3,'') # Insert blank
  19.         llist.insert(4,'') # Insert blank
  20.     x = '' # Empty string x
  21.     x = x.join([i + ' ' for i in llist[6:]]).strip() # Join each list element in positions > 6
  22.     nlist = llist[:6] # Add first 6 listings
  23.     nlist.append(x) # Add tradename
  24.     vtuple = tuple(nlist) # Covert list to tuple
  25.     tlist.append(vtuple) # Add listing tuple to list
  26. tstring = tstring.join([str(i) + ",\n" for i in tlist]) # Join each listing with a comma
  27. w.write(str(tstring[:-2]) + ";") # Remove last newline and comma, add semicolon, write to file
  28. w.close()
  29.  

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()

Permalink: http://codedumper.com/ujeca