gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:07:30 25 4
gpt4 key购买 nike

我在 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: expected string or buffer。问题在于 hashtags = re.findall(r"#(\w+)", i)。

有什么建议吗?

谢谢!

最佳答案

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

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

希望对您有所帮助。

关于python - 使用 python MySQLdb 解析 MySQL 数据库以提取主题标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21198130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com