2014-01-17 17 views
1

我在MySQL數據庫中挖掘了推文,並且我設法連接到它並查詢包含推文文本的列。現在我想要做的就是解析這個並將hashtags提取到一個csv文件中。使用python MySQLdb解析MySQL數據庫以提取井號標籤

到目前爲止,我有這樣的代碼,該代碼工作,直到最後一個循環:

import re 
import MySQLdb 

# connects to database 
mydb = MySQLdb.connect(host='****', 
    user='****', 
    passwd='****', 
    db='****') 
cursor = mydb.cursor() 

# queries for column with tweets text 
getdata = 'SELECT text FROM bitscrape' 
cursor.execute(getdata) 
results = cursor.fetchall() 

for i in results: 
    hashtags = re.findall(r"#(\w+)", i) 
    print hashtags 

我得到以下錯誤:類型錯誤:預期的字符串或緩衝區。問題在於hashtags = re.findall(r「#(\ w +)」,i)。

有什麼建議嗎?

謝謝!

回答

0

cursor.fetchall()返回列表元組。從各行採取的第一個元素,並將其傳遞給findall()

for row in results: 
    hashtags = re.findall(r"#(\w+)", row[0]) 

希望有所幫助。