Created
September 17, 2012 00:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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