【发布时间】:2017-12-23 09:20:36
【问题描述】:
我正在尝试将 Oracle 12c 数据库中保存的表导出到组成 XML 文件中,这样 Oracle 表中的每一行都会生成 1 个 XML 文件。为此,我使用了 Python 2.7 库 xml.etree.ElementTree,但我在 documentation 中看不到任何允许我执行此操作的内容。基本上我现在需要的是:
import cx_Oracle
from xml.etree import ElementTree as ET
SQL = ''.join([ 'SELECT * FROM ', table_name ])
connection = cx_Oracle.connect('username/password@database')
cursor = connection.cursor()
for i in range(num_rows):
... #code works fine up to here
file_name = i
file_path = ''.join([ 'C:\..., file_name, '.xml ])
file = open(file_path, 'w')
cursor.execute(SQL)
ET.ElementTree.write(file) #This line won't work
cursor.close()
file.close()
connection.close()
我知道这只会是 1 行代码 - 我真的不知道该怎么做。
作为一个额外的复杂因素,我只能使用 Python 2.7 的原生库,例如 etree - 我无法在工作中下载第 3 方 Python 库。提前感谢您的任何帮助或建议。
【问题讨论】:
-
write()是ElementTree实例上的方法,但您在类上调用它。代表表格行的 XML 结构的根元素应该是一个参数。ET.ElementTree(root).write(file)是否有效(假设root是那个根元素)?
标签: python xml oracle python-2.7 elementtree