使用python MySQLdb解析MySQL数据库以提取主题标签

时间:2014-01-17 23:53:45

标签: python mysql parsing mysql-python tweets

我在MySQL数据库中抓取了推文,我设法连接到它并查询包含推文文本的列。现在我要做的是解析这个并将主题标签提取到csv文件中。

到目前为止,我的代码一直工作到最后一个循环:

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

我收到以下错误:TypeError:期望的字符串或缓冲区。问题在于hashtags = re.findall(r“#(\ w +)”,i)。

有什么建议吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

cursor.fetchall()会返回元组列表。从每行获取第一个元素并将其传递给findall()

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

希望有所帮助。

相关问题