[英]How to export a table to csv or excel format
我需要將oracle表導出為csv / excel文件格式(以及列標題)。 通過cx_oracle或通過sqlplus歡迎的解決方案。
評論中的Python代碼:
con = cx.connect()
cur = con.cursor()
printer = cur.execute(sqlcode)
con.commit()
也許使用csv模塊(來自標准庫):
import csv
cursor = connection.cursor() # assuming you know how to connect to your oracle db
cursor.execute('select * from table_you_want_to_turn_to_csv')
with open('output_file.csv', 'wb') as fout:
writer = csv.writer(fout)
writer.writerow([ i[0] for i in cursor.description ]) # heading row
writer.writerows(cursor.fetchall())
如果您有大量數據,請將fetchall()展開到循環中。
快樂的小道!
要創建XLS文件,請使用python-excel項目中的xlwt模塊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.