2
所以我一直在嘗試從使用熊貓和python的mysql數據庫創建數據框,但是我遇到了一個我需要幫助的問題。熊貓從mysql數據庫創建數據框
問題是,當寫入數據幀到excel時,它只寫入最後一行,即覆蓋所有以前的條目,並且只寫入最後一行。請參閱下面的代碼
import pandas as pd
import numpy
import csv
with open('C:path_to_file\\extract_job_details.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
jobid = str(row[1])
statement = """select jt.job_id ,jt.vendor_data_type,jt.id as TaskId,jt.create_time as CreatedTime,jt.job_start_time as StartedTime,jt.job_completion_time,jt.worker_path, j.id as JobId from dspe.job_task jt JOIN dspe.job j on jt.job_id = j.id where jt.job_id = %(jobid)s"""",
df_mysql = pd.read_sql(statement1, con=mysql_cn)
try:
with pd.ExcelWriter(timestr+'testResult.xlsx', engine='xlsxwriter') as writer:
df_mysql.to_excel(writer, sheet_name='Sheet1')
except pymysql.err.OperationalError as error:
code, message = error.args
mysql_cn.close()
請任何人都可以幫助我確定我要去哪裏錯了嗎? PS我是熊貓和蟒蛇的新手。 感謝卡洛斯
您定義'statement'但不知你逝去'statement1'到'pd.read_sql'功能。這可能是你的問題嗎? – Abdou