gpt4 book ai didi

python - 使用 Python 将 MySQL 表中的数据保存到文本文件

转载 作者:行者123 更新时间:2023-11-29 05:32:13 27 4
gpt4 key购买 nike

所以我有一个 mysql 表,我试图从表的一个字段中获取每个元素。字段和表都称为“关键字”。在现场有许多不同的随机词,我正在尝试将所有这些词保存到一个文本文件中。任何关于如何实现这一点的帮助都会很棒,这是我目前所拥有的。

#!/usr/bin/env python
import MySQLdb


db = MySQLdb.connect(host="", user="", passwd="", db="")
cursor = db.cursor()
sql = """SELECT DISTINCT keywords FROM keywords"""
cursor.execute(sql)
cursor.fetchall()
db.close()

for s in sql:
tweets = open("keywords.txt", "w")

我当时的想法是,如果可能的话,将 sql 获取的内容转换成一个列表,然后将其写入文件。但我愿意接受任何建议,谢谢。

最佳答案

像这样的东西应该可以工作:

import MySQLdb

db = MySQLdb.connect(host="", user="", passwd="", db="")
cursor = db.cursor()
sql = """SELECT DISTINCT keywords FROM keywords"""
tweets = open("keywords.txt", "w")
cursor.execute(sql)
for row in cursor:
print>>tweets, row[0]
tweets.close()
db.close()

关于python - 使用 Python 将 MySQL 表中的数据保存到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13834460/

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