【发布时间】:2019-12-20 04:55:22
【问题描述】:
对于工作中的项目,我需要从 teradata 导出到 excel 一个 sql 请求。
到目前为止,我已成功将多个请求完全导出到我的 excel 文件中,但输出格式并不完全符合我的要求。实际上,输出不尊重原始表,只是逐行给出了连接,例如: 一种 1('000000000DUM','RRFOR','','','NOTE_CONTREP','20.00') 我想要
A1 :000000000DUM B1 : RRFOR C1 :'' D1 : '' E1 : NOTE_CONTREP F1 : 20.00
这是我的代码:
def KPI_MAKER(instid):
conn=pyodbc.connect('DRIVER={Teradata};DBCNAME=FTGPRDTD;UID='+str('L291506')+';PWD='+str('Pilote06!')+';QUIETMODE=YES;')
Agg=str("sel code_gen1,code_gen2,cd_prd_cpta,lieu_stkph_cd,don_source_cd,incorrect_value from DB_FTG_SRS_DATALAB.mdc_ctrl_anomalie where rg_no = 'RGC-TIE-012'")
cursor = conn.cursor()
cursor.execute(Agg)
Agg_output = cursor.fetchall()
conn.close
df = pd.DataFrame(Agg_output)
writer = pd.ExcelWriter('KPI_FORTIS_13082019.xlsx', engine='xlsxwriter')
df.to_excel(writer, index=False, sheet_name='POC_output') # sheet 0
writer.save()
print('done')
【问题讨论】:
标签: python excel pandas teradata xlsxwriter