【发布时间】:2016-10-19 06:28:58
【问题描述】:
基于给定的 SQL 语句,我使用以下函数从数据库中提取到 CSV:
def extract_from_db():
with open('myfile.csv','w') as outfile:
for row in cursor:
outfile.write(str(row[0])+";"+str(row[1])+";"+str(row[2])+";"+str(row[3])+";"+str(row[4])+";"+str(row[5])
+";"+str(row[6])+";"+str(row[7])+";"+str(row[8])+";"+str(row[9])+";"+str(row[10])+";"+str(row[11])+";"+str(row[12])
+";"+str(row[13])+";"+str(row[14])+"\n")
我怎样才能在文件的开头为可变数量的列写列名,这样我就不必硬编码了?硬编码的连接也很丑陋。
【问题讨论】:
标签: python database csv export-to-csv cx-oracle