Я хотел бы добавить несколько узлов "брата" после узла "прокси" в файле xml. Это то, что я написал:
import os
from twb_traduction import parse_twb
from lxml import etree
def retrieve_proxy_parent_node(twb_path_source,proxy,key,value):
tree, root = parse_twb.get_root_tree(twb_path_source)
nodes = parse_twb.retrieve_node_by_text(root, tree, proxy)
def parse(tree,root,nodes):
alias_parent = [parse_twb.get_parent(node, 1) for node in nodes]
for aliases in alias_parent:
etree.SubElement(aliases, 'alias', key=key, value=value)
print(etree.tostring(alias_parent[0]))
root.write("mon_test.twb", encoding='utf-8', xml_declaration=True, pretty_print=True)
print('done')
parse(tree,root,nodes)
print()
if __name__ == '__main__':
############## Parameters #####################
# 1-
input_dir = r'..\..\..\BI\Analyse_Air'
output_dir = 'data\output\output_demo'
views_twbsources = [('Analyse Air', 'QAL_Analyse Air - Copie.twb')]
for view, twb_name_source in views_twbsources:
input_filepath = os.path.join(input_dir, twb_name_source)
key = r"'Allemagne'"
retrieve_proxy_parent_node(input_filepath,proxy="Alias_Dep",key=r"'Allemagne'",value="Alias_Dep_Allemagne")
Вот результат:
<aliases xmlns:user="http://www.tableausoftware.com/xml/user">
<alias key=""Albanie"" value="Alias_Dep_Albanie"/>
<alias key=""Algérie"" value="Alias_Dep_Algérie"/>
<alias key="\'Allemagne\'" value="Alias_Dep_Allemagne"/>
<alias key="\'Allemagne\'" value="Alias_Dep_Allemagne"/>
</aliases>
Как вы видите, печатное шоу key="\'Allemagne\'"
вместо key=""e;Allemagne"e;"
Я пробовал другую комбинацию, но ни одна из них не работает, как вы видите:
# 1
retrieve_proxy_parent_node(input_filepath,proxy="Alias_Dep",key=r""Allemagne"",value="Alias_Dep_Allemagne") # give for Allemagne: <alias key="&quot;Allemagne&quot;" value="Alias_Dep_Allemagne"/></aliases>
# 2
retrieve_proxy_parent_node(input_filepath,proxy="Alias_Dep",key=r""Allemagne"",value="Alias_Dep_Allemagne") # give for Allemagne: <alias key="&quot;Allemagne&quot;" value="Alias_Dep_Allemagne"/></aliases>\n