【问题标题】:parsing MySQL database with python MySQLdb to extract hashtags使用 python MySQLdb 解析 MySQL 数据库以提取主题标签
【发布时间】:2014-01-17 23:53:45
【问题描述】:

我在 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) 行中。

有什么建议吗?

谢谢!

【问题讨论】:

    标签: python mysql parsing mysql-python tweets


    【解决方案1】:

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

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

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多