我在Python 2.7中使用读取URL文件的方式,构建了一个基于6个变量的sqlite db和table。
我使用了JSON并创建了一个字典。代码可以很好地读取所有内容,并循环遍历键和值。
我需要将这些内容插入到我的表格中。这就是我有点困惑的地方。我会提供代码,我认为问题会很明显。
import json
import urllib2
#Read file and print a line
webFD=urllib2.urlopen("http://rasinsrv07.cstcis.cti.depaul.edu/CSC455/assignment4.txt")
tweet = webFD.readline()
tweet
#create dictionary
dictt=json.loads(tweet)
#print dictionary
dictt.keys()
#print values
dictt.values()
#loop through tweets
for (key, value) in dictt.items():
print key, '->', value
#Created the DB
import sqlite3
conn = sqlite3.connect('twitter.db')
c = conn.cursor()
#Created the table for the tweets
c.execute("CREATE TABLE Tweet(created_at, id, text, source, in_reply_to_user_ID,retweet_Count)")
这里是我的问题所在。想要将这些推文(字典中的6个键和值)加载到推文表中:
for elt in tweet:
currentRow = elt[:-1].split(", ")
insert = """insert into Tweet values ('%s', '%s', '%s', '%s', '%s', '%s')""" %("created_at", "id", "text", 'source', 'in_reply_to_user_ID', 'retweet_Count')
print insert