嘿,我只是想知道是否有人能够帮助编写一系列文本文件的内容(例如activity.txt、sports.txt)(每个文件都有一个数字、一个选项卡和一个值(例如每排 90 个 Feather 球)。我已经初始化了数据库连接并准备好 SQL 中的表。
任何帮助将不胜感激!
谢谢
最佳答案
如果我理解正确,那么下面的代码片段应该可以帮助您入门。我假设您已经可以访问光标...
category = "Sports"
with open("sports.txt", "r") as sports:
lines = sports.readlines()
for line in lines:
# Split the line on whitespace
data = line.split()
number = data[0]
value = data[1]
# Put this through to SQL using an INSERT statement...
cursor.execute("""INSERT INTO tablename (person_id, category, type)
VALUES(%s, %s, %s)""", (number, category, value))
关于python - 使用 Python 将文本文件写入 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6146729/