查询DB表并转换为DataFrame ok,但数据在导出时被截断

2024-09-22 20:20:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在查询MySQL数据库并从表中获取数据。我的代码看起来像这样

# login creds, etc., etc., 

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM myTable")

我想,一切似乎都正常,但当我将数据导出到文本文件时

myresult = mycursor.fetchall()

df = pd.DataFrame(myresult)
#print(df)
df=str(df)

outF = open("C:\\Users\\ryans\\OneDrive\\Desktop\\test.txt", "w")
outF.writelines(df)
outF.close()

Tags: 代码from数据库dfexecutemysqletclogin