python - XML特定行配对python
问题描述
我有一个 XML 文件示例,我必须构建类似的 xml 文件。
<datasource formatted-name='federated.1819qwi0hys5391dzxhl70o95li4' inline='true' source-platform='win' version='18.1' xmlns:user='http://www.tableausoftware.com/xml/user'>
<connection class='federated'>
<named-connections>
<named-connection caption='Sample - Superstore' name='excel.1ew9u4t0tggb9315darmm0nfz2kb'>
<connection class='excel' driver='' filename='C:/Users/XXXX/Downloads/Sample - Superstore.xls' header='yes' imex='1' password='' server='' />
</named-connection>
</named-connections>
<relation connection='excel.1ew9u4t0tggb9315darmm0nfz2kb' name='Custom SQL Query' type='text'>SELECT [Orders$].[Category] AS [Category], [Orders$].[City] AS [City], 
</relation>
我必须阅读关系连接sql查询并动态写入如下xml输出。
<metadata-record class='column'>
<remote-name>Category</remote-name>
<remote-type>130</remote-type>
<local-name>[Category]</local-name>
<parent-name>[Custom SQL Query]</parent-name>
</metadata-record>
<metadata-record class='column'>
<remote-name>City</remote-name>
<remote-type>130</remote-type>
<local-name>[City]</local-name>
<parent-name>[Custom SQL Query]</parent-name>
</metadata-record>
我已经建立了可以硬编码的关系连接,但是元数据记录应该通过读取关系连接中的 sql 查询来动态构建。那是期望。粘贴下面的完整代码
import xml.etree.cElementTree as et
import pandas as pd
colAtt[]
datasource = et.Element("datasource")
datasource.set("formatted-name",'federated.1819qwi0hys5391dzxhl70o95li4')
datasource.set("inline",'True')
datasource.set("source-platform",'win')
datasource.set("version",'18.1')
datasource.set("xmlns:user",'http://www.tableausoftware.com/xml/user')
connection = et.SubElement(datasource, "Connection")
connection.set("Class", "federated")
namedconnections = et.SubElement(connection, "named-connections")
namedconnection = et.SubElement(namedconnections, "namedconnection")
namedconnection.set("caption", 'Sample - Superstore')
namedconnection.set("name", 'excel.1ew9u4t0tggb9315darmm0nfz2kb')
connection=et.SubElement(namedconnection, "connection")
connection.set("class", 'excel')
connection.set("driver", ' ')
connection.set("filename", 'C:/Users/XXXX/Downloads/Sample - Superstore.xls')
connection.set("header", 'yes')
connection.set("imex", '1')
connection.set("password", ' ')
connection.set("server", ' ')
relationconnection=et.SubElement(datasource, "relation")
relationconnection.set('Connection','excel.1ew9u4t0tggb9315darmm0nfz2kb')
relationconnection.set('name ','Custom SQL Query')
relationconnection.set('type','text')
relationconnection.text='SELECT [Orders$].[Category] AS [Category],
,
, [Orders$].[City] AS [City],
,

metadatarecords = et.SubElement(datasource, "metadata-records")
for x in datasource.findall('relation'):
#print(x.tag,x.attrib,x.text)
colAtt.append(x.text)
.
但它附加了整个文本,我不知道如何分别拆分类别和城市。有人可以帮忙吗?
解决方案
推荐阅读
- python - 从多个线程同时调用 .net 库中的方法时结果不完整
- sql - 分区表中的死锁
- azure - 将 AbortOnConnectFail 设置为 false,我们仍然会在 Azure REDIS 上获得 ConnectTimeout
- sql - SQL 数据库在 ASC 中订购多于一列
- javascript - 将输入推送到数组列表并使用新的更新内容
- amazon-web-services - 如何使用 Ansible Tower/AWX 将 Kubernetes YAML 文件应用到 AWS EKS?
- c# - 优化此代码并找到每个 ROW/COLUMN 的 MIN 和 MAX 值的最佳、正确方法是什么
- jenkins - 如何在 Git 提交时触发 Jenkins 流水线
- spring - Spring Sleuth 获取 POST 请求异常
- java - 从对话框自定义布局中获取值