[英]How to export exact format table from teradata to excel?
對於工作中的項目,我需要從 teradata 導出以處理 sql 請求。
到目前為止,我設法將幾個請求完全導出到我的 excel 文件中,但輸出格式並不是我想要的。 事實上,輸出不尊重原始表,只是逐行給出一個連接,如:A 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')
問題是fetchall
方法返回一個行對象列表,而不是一個元組列表。 添加從行到元組的 Agg_output 轉換將允許DataFrame
方法執行您想要的操作。 但是為什么要打擾光標呢? 只需讓熊貓直接從查詢中創建數據框:
df = pd.read_sql(Agg,conn)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.