Skip to content

Instantly share code, notes, and snippets.

@ngopal
Created September 17, 2012 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngopal/3734990 to your computer and use it in GitHub Desktop.
Save ngopal/3734990 to your computer and use it in GitHub Desktop.
a script used to parse a txt file I found on the internet.
import sqlite3
file = open('quotes_file.txt', 'r')
line = ''
quotes = []
for i in file.readlines():
if i == '\r\n':
print "BLANK", i
quotes.append(line)
line = ''
else:
print "CONTENT", i
line = line + ' ' + i.strip('\r\n')
conn = sqlite3.connect('quotes.db')
c = conn.cursor()
c.execute('''CREATE TABLE quotes(id int, quote text)''')
for i in range(len(quotes)):
cleaned_string = quotes[i].replace("'", r"\'")
comm = "INSERT INTO quotes VALUES ("+str(i)+", \'"+str(cleaned_string)+"\')"
print i, quotes[i]
print comm
try:
c.execute(comm)
except:
print 'apostraphe error'
pass
conn.commit()
c.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment