[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
[[ノート/テキストマイニング]]~
訪問者数 &counter(); 最終更新 &lastmod();~
**MySQLにあるツイートをLIKEを使って数える --- 2012/10/21 [#y82cfbcd]
SQL構文のうちの、LIKEを使って、「XXを含むツイート」を数えてみる
まず、LIKEの使い方は
SELECT text FROM <table> WHERE text LIKE '%<文字列>%'
但し、%の部分は任意文字列にマッチする
マッチするレコード(=ツイート)を取り出すプログラムは下記の通り。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# dbcountwithLIKE.py
#
import sys
import codecs
import time
import MySQLdb
import string
from dbinit import dbinit
sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
con = dbinit() # 自作関数dbinitはmySQLdbconnect(db, host, port, user, passwd)を呼ぶ。
cur = con.cursor()
tablename = 'tw12102112'
mystring = u'%電車%' # LIKEの対象文字列
#mystring = u'%今日%'
s = "SELECT text FROM " + tablename + " WHERE text LIKE '" + mystring.encode('utf_8') + "'"
cur.execute(s)
r = cur.fetchone()
while r != None:
print "---" + r[0].encode('utf_8')
r = cur.fetchone()
cur.close()
con.close()
もちろん、データベースからすべてのツイートを順繰りに取り出してからそれぞれをPython内でいろいろな処理をしてもよいのだが、SQLの機能を使ってみるとこうなるという例。
この方法で、文字列XXを含むツイートの数を数える(SQLに数えさせる)には、
SELECT COUNT(*) FROM <table> WHERE text LIKE '%<文字列>%'
をすればよい。
SQLの結果はカウント数(int)なので、printするにはstrを取ってやった方がいい。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import codecs
import time
import MySQLdb
import string
from dbinit import dbinit
sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
con = dbinit()
cur = con.cursor()
tablename = 'tw12102112'
mystring = u'%電車%'
#mystring = u'%今日%'
#s = "SELECT text FROM " + tablename + " WHERE text LIKE '" + mystring.encode('utf_8') + "'"
s = "SELECT COUNT(*) FROM " + tablename + " WHERE text LIKE '" + mystring.encode('utf_8') + "'" # ここを書き換えた
cur.execute(s)
r = cur.fetchone()
while r != None:
print str(r[0]).encode('utf_8') # printにstrを噛ませた
r = cur.fetchone()
cur.close()
con.close()
終了行:
[[ノート/テキストマイニング]]~
訪問者数 &counter(); 最終更新 &lastmod();~
**MySQLにあるツイートをLIKEを使って数える --- 2012/10/21 [#y82cfbcd]
SQL構文のうちの、LIKEを使って、「XXを含むツイート」を数えてみる
まず、LIKEの使い方は
SELECT text FROM <table> WHERE text LIKE '%<文字列>%'
但し、%の部分は任意文字列にマッチする
マッチするレコード(=ツイート)を取り出すプログラムは下記の通り。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# dbcountwithLIKE.py
#
import sys
import codecs
import time
import MySQLdb
import string
from dbinit import dbinit
sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
con = dbinit() # 自作関数dbinitはmySQLdbconnect(db, host, port, user, passwd)を呼ぶ。
cur = con.cursor()
tablename = 'tw12102112'
mystring = u'%電車%' # LIKEの対象文字列
#mystring = u'%今日%'
s = "SELECT text FROM " + tablename + " WHERE text LIKE '" + mystring.encode('utf_8') + "'"
cur.execute(s)
r = cur.fetchone()
while r != None:
print "---" + r[0].encode('utf_8')
r = cur.fetchone()
cur.close()
con.close()
もちろん、データベースからすべてのツイートを順繰りに取り出してからそれぞれをPython内でいろいろな処理をしてもよいのだが、SQLの機能を使ってみるとこうなるという例。
この方法で、文字列XXを含むツイートの数を数える(SQLに数えさせる)には、
SELECT COUNT(*) FROM <table> WHERE text LIKE '%<文字列>%'
をすればよい。
SQLの結果はカウント数(int)なので、printするにはstrを取ってやった方がいい。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import codecs
import time
import MySQLdb
import string
from dbinit import dbinit
sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
con = dbinit()
cur = con.cursor()
tablename = 'tw12102112'
mystring = u'%電車%'
#mystring = u'%今日%'
#s = "SELECT text FROM " + tablename + " WHERE text LIKE '" + mystring.encode('utf_8') + "'"
s = "SELECT COUNT(*) FROM " + tablename + " WHERE text LIKE '" + mystring.encode('utf_8') + "'" # ここを書き換えた
cur.execute(s)
r = cur.fetchone()
while r != None:
print str(r[0]).encode('utf_8') # printにstrを噛ませた
r = cur.fetchone()
cur.close()
con.close()
ページ名: