Tip: Click lines to highlight, hold ctrl/cmd to multi-select
SQL (10-Aug @ 18:25)
Syntax Highlighted 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()
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()