Python-将Oracle表导出到CSV错误

时间:2020-03-20 07:11:13

标签: python oracle

我有一个Python脚本,可将Oracle表转换为CSV:

import sys
import csv
import cx_Oracle
import time
from datetime import timedelta
start_time = time.monotonic()
table_name = sys.argv[1]
csv_file_name = table_name + ".csv"
sql_query = "SELECT * FROM " + table_name
con = cx_Oracle.connect('abc/password@ec2-1-111-11-111.compute-1.amazonaws.com/orcl')
cursor = con.cursor()
csv_file = open(csv_file_name, "w")
writer = csv.writer(csv_file, delimiter=',', lineterminator="\n", quoting=csv.QUOTE_NONE)
r = cursor.execute(sql_query)
for row in cursor:
    writer.writerow(row)
cursor.close()
con.close()
csv_file.close()
end_time = time.monotonic()
print(timedelta(seconds=end_time - start_time))

当我跑步时:

python3 export-csv.py AUTHOR

它给了我这个错误:

Traceback (most recent call last):
  File "export-csv.py", line 16, in <module>
    writer.writerow(row)
_csv.Error: need to escape, but no escapechar set

如何解决此问题?另外,如何获取CSV文件中包含的Oracle表的列名?

0 个答案:

没有答案