简体   繁体   English

将带有列名的 Oracle 数据库列标题导出到 csv

[英]Export Oracle database column headers with column names into csv

I am making a program that fetches column names and dumps the data into csv format.我正在制作一个获取列名并将数据转储为 csv 格式的程序。 Now everything is working just fine and data is being dumped into csv, the problem is, I am not able to fetch headers into csv.现在一切正常,数据正在转储到 csv 中,问题是,我无法将标题提取到 csv 中。 If I open the exported csv file into excel, only data shows up not the column headers.如果我将导出的 csv 文件打开到 excel 中,则只显示数据而不显示列标题。 How do I do that?我怎么做?

Here's my code:这是我的代码:

import cx_Oracle
import csv

dsn_tns = cx_Oracle.makedsn(--Details--)
conn = cx_Oracle.connect(--Details--)

d = conn.cursor()

csv_file = open("profile.csv", "w")
writer = csv.writer(csv_file, delimiter=',', lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC)
d.execute("""
select * from all_tab_columns where OWNER = 'ABBAS'
""")
tables_tu = d.fetchall()
for row in tables_tu:
    writer.writerow(row)

conn.close()
csv_file.close()

What code do I use to export headers too in csv?我也使用什么代码在 csv 中导出标题?

Place this just above your for loop:把它放在你的for循环之上:

writer.writerow(i[0] for i in d.description)

Because d.description is a read-only attribute containing 7-tuples that look like:因为d.description是包含 7 元组的只读属性,如下所示:

(name, 
type_code, 
display_size,
internal_size, 
precision, 
scale, 
null_ok)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM