pythons sqlite3モジュールを使用しているときに、テーブルを作成し、最初の行に4列と表示されていた場合、次の行には4列が必要ですか、それとも多かれ少なかれありますか?
語彙のデータベースを作成したいと思っています。各単語には、さまざまな数の定義があります。
たとえば、「set」には「panacea」よりもはるかに多くの定義があります。
この語彙データベースは、辞書参照サイトから単語と定義を簡単に検索できるスクレーパーを使用して作業します。
#! /usr/bin/env python
import mechanize
from BeautifulSoup import BeautifulSoup
import sys
import sqlite3
def dictionary(word):
br = mechanize.Browser()
response = br.open('http://www.dictionary.reference.com')
br.select_form(nr=0)
br.form['q'] = word
br.submit()
definition = BeautifulSoup(br.response().read())
trans = definition.findAll('td',{'class':'td3n2'})
fin = [i.text for i in trans]
query = {}
for i in fin:
query[fin.index(i)] = i
## The code above is given a word to look up and creates a 'dict' of its definiton from the site.
connection = sqlite3.connect('vocab.db')
with connection:
spot = connection.cursor()
## This is where my uncertainty is. I'm not sure if I should iterate over the dict values and 'INSERT' for each definition or if there is a way to put them in all at once?
spot.execute("CREATE TABLE Words(Name TEXT, Definition TEXT)")
spot.execute("INSERT INTO Words VALUES(word, Definition (for each number of definitions))")
return query
print dictionary(sys.argv[1])
これは課題ではありませんが、sqlite3を学習するための個人的な演習です。