如何将teradata的精确格式表导出到excel?

2024-09-28 05:25:43 发布

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

对于工作中的项目,我需要从teradata导出一个sql请求到excel。你知道吗

到目前为止,我设法完全导出几个请求到我的excel文件,但输出格式不完全是我要找的。实际上,输出并不尊重原始表,只是逐行给出一个串联,如: A 1('000000000 dum'、'RRFOR'、''、'、'NOTE\u CONTREP'、'20.00') 我想要

A1:000000000 DUM B1:RRC1:“”D1:“”E1:注\U 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')

谢谢各位

凯尔0


Tags: 项目dfoutputcdcodeconnexcelcursor
1条回答
网友
1楼 · 发布于 2024-09-28 05:25:43

问题是fetchall方法返回的是对象列表,而不是元组列表。添加从行到元组的Agg\u输出的转换将允许DataFrame方法执行您想要的操作。但为什么要用光标呢?只需直接从查询创建数据帧:

df = pd.read_sql(Agg,conn)

相关问题 更多 >

    热门问题