使用Python将多个文本替换为其相应的XML文本-第2部分- [英] Replace multiple texts with their corresponding texts in XML using Python - Part 2 -

查看:72
本文介绍了使用Python将多个文本替换为其相应的XML文本-第2部分-的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想替换别名元素中带有名称字典中的对应键。

I would like to replace a text of aliasname element with a text from name if there is no corresponding key in the dictionary.

这是我现在正在处理的xml。

here is the xml that I am working on now.

- <esri:Workspace xmlns:esri="http://www.esri.com/schemas/ArcGIS/10.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">  
  - <WorkspaceDefinition xsi:type="esri:WorkspaceDefinition">   
         <WorkspaceType>esriLocalDatabaseWorkspace</WorkspaceType>       
         <Domains xsi:type="esri:ArrayOfDomain" />  
  - <DatasetDefinitions xsi:type="esri:ArrayOfDataElement">   
          - <DataElement xsi:type="esri:DEFeatureClass">   
            <CatalogPath>/FC=CHO_H12</CatalogPath>      
            <Name>CHO_H12</Name>      
            <DatasetType>esriDTFeatureClass</DatasetType>
            <DSID>8</DSID>      
            <Versioned>false</Versioned>  
            <CanVersion>false</CanVersion>
            <ConfigurationKeyword />
            <HasOID>true</HasOID>
            <OIDFieldName>OBJECTID</OIDFieldName>
            - <Fields xsi:type="esri:Fields"> 
               - <FieldArray xsi:type="esri:ArrayOfField">     
                  - <Field xsi:type="esri:Field">  
                         <Name>KEY_CODE</Name>   
                         <Type>esriFieldTypeString</Type>
                         <IsNullable>true</IsNullable> 
                         <Length>255</Length> 
                         <Precision>0</Precision>
                         <Scale>0</Scale>  
                         <AliasName>リンクコード</AliasName>   # to "KEY_CODE" 
                         <ModelName>KEY_CODE</ModelName>        
                     </Field>
                  - <Field xsi:type="esri:Field"> 
                          <Name>KEN</Name>
                          <Type>esriFieldTypeString</Type> 
                          <IsNullable>true</IsNullable>
                          <Length>255</Length>
                          <Precision>0</Precision>
                          <Scale>0</Scale>
                          <AliasName>都道府県番号</AliasName>    # to "Prefecture_Code"
                          <ModelName>KEN</ModelName>          
                     </Field>
                  - <Field xsi:type="esri:Field">
                           <Name>CITY</Name>          
                           <Type>esriFieldTypeString</Type>          
                           <IsNullable>true</IsNullable>          
                           <Length>255</Length>          
                           <Precision>0</Precision>          
                           <Scale>0</Scale>          
                           <AliasName>市区町村番号</AliasName>   # to "City_code"      
                           <ModelName>CITY</ModelName>          
                           </Field>

我写道;

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
import xml.etree.cElementTree as etree

jp2en = {'リンクコード': 'KEY_CODE', '都道府県番号': 'Prefecture_Code',
'市区町村番号': 'City_Code'} 

    tree = etree.parse('input.xml')
    for field in tree.iterfind('.//Fields/FieldArray/Field'):
         name, alias = [field.find(tag) for tag in ['Name', 'AliasName']]
         if name is None or alias is None: continue
         alias.text = jp2en.get(name.text.strip(), name.text)
    tree.write('output.xml', encoding='utf-8')

但是,对于输出,将替换 aliasname 元素的文本通过来自名称的问题,即使存在

However, for the output, a text of aliasname element is replaced by a text from name even when there is corresponding key in the dictionary, jp2en like this;

<AliasName>リンクコード</AliasName>   Returns a text "KEY_CODE" # the text is the same for Name and a key. 
<AliasName>都道府県番号</AliasName>   Returns a text "KEN" from Name instead of a key "Prefecture_Code"
<AliasName>市区町村番号</AliasName>   Returns a text "CITY" from Name instead of a key "City_code"

我使用python 3.2.2 。

I use python 3.2.2.

推荐答案

我猜(看到当前行为不是您想要的),您想替换<如果字典中没有相应的键,则code>< AliasName> 元素中带有< Name> 中的文本。更改此行(如果没有键,则不会更改< AliasName> ):

I guess (seeing that the current behaviour is not what you want) you'd like to replace a text of <AliasName> element with a text from <Name> if there is no corresponding key in the dictionary. Change this line (which doesn't change <AliasName> if there is no key):

 alias.text = jp2en.get(name.text.strip(), alias.text)

收件人:

 alias.text = jp2en.get(name.text.strip(), name.text)

如果键值应来自< AliasName> 而不是< Name> ,然后将上面的行替换为:

If key values should come from <AliasName> instead of <Name> then replace the above line by:

 if alias.text is not None:
    alias.text = jp2en.get(alias.text.strip(), name.text)
 else:
    alias.text = name.text

这篇关于使用Python将多个文本替换为其相应的XML文本-第2部分-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆