简体   繁体   English

如何使用美丽的汤和 python 更新 xml 标签内的名称

[英]how to update the names inside the tags of xml using beautiful soup and python

I am tring to change the value of the name inside parent tag whenever there are Z- in the last of the name.每当名称的最后一个有 Z- 时,我都会尝试更改父标记内名称的值。 by taking the value from my table, let say there are only three such pattern where name ends with Z- and I have values to update in list val.通过从我的表中获取值,假设只有三个这样的模式,其中名称以 Z- 结尾,并且我在列表 val 中有要更新的值。

how can I do that?我怎样才能做到这一点?

lets say this is my XML,假设这是我的 XML,

<parent name="A-6/Z-9/B-64/Z-12">
<item>
<list>
<p name="A">
<p name ="B">
</list>
</item>
</parent>
<parent name="A1/Zh/B/C-12">
<item>
<list>
<p name="A">
<p name ="B">
</list>
</item>
</parent>
<parent name="AS-5/ZD-9/B-67/Z-13">
<item>
<list>
<p name="A">
<p name ="B">
</list>
</item>
</parent>
<parent name="An4/Zd-8/B-5/C-13">
<item>
<list>
<p name="A">
<p name ="B">
</list>
</item>
</parent>
<parent name="A-76/Z-8/B-56/Z-14">
<item>
<list>
<p name="A">
<p name ="B">
</list>
</item>
</parent>
<parent name="A-45/Z-ty/B-9/C-14">
<item>
<list>
<p name="A">
<p name ="B">
</list>
</item>
</parent>

this is my values in list val = ["Z-99","Z-98","Z-97"]这是我在列表 val = ["Z-99","Z-98","Z-97"]

I want to fix these values in my XML as whenever parent have name ends with Z for example these我想在我的 XML 中修复这些值,因为只要父母的名字以 Z 结尾,例如这些

<parent name="A-6/Z-9/B-64/Z-12">
<parent name="AS-5/ZD-9/B-67/Z-13">
<parent name="A-76/Z-8/B-56/Z-14">

I want this我要这个

<parent name="A-6/Z-9/B-64/Z-99">
<parent name="AS-5/ZD-9/B-67/Z-98">
<parent name="A-76/Z-8/B-56/Z-97">

I tried these but nothing is working for me我尝试了这些,但没有什么对我有用

pattern = re.compile(r'[A-Z]+-+[0-9]+/+Z+-+[0-9]$')
for i in soup.find_all('parent', distName=pattern):
       for j in val:
           i.string = i.string[:-2]+str(j)


 for i, val in zip(soup.select('parent > name^="Z-"')
         i.string = i.string[:-2]+str(val)

You can try this script to change all required name= attributes from <parent> tag:您可以尝试使用此脚本从<parent>标记更改所有必需的name=属性:

from bs4 import BeautifulSoup


txt = '''<parent name="A-6/Z-9/B-64/Z-12">
<item>
<list>
<p name="A"></p>
<p name="B"></p>
</list>
</item>
</parent>

<parent name="A1/Zh/B/C-12">
<item>
<list>
<p name="A"></p>
<p name="B"></p>
</list>
</item>
</parent>

<parent name="AS-5/ZD-9/B-67/Z-13">
<item>
<list>
<p name="A"></p>
<p name="B"></p>
</list>
</item>
</parent>

<parent name="An4/Zd-8/B-5/C-13">
<item>
<list>
<p name="A"></p>
<p name="B"></p>
</list>
</item>
</parent>

<parent name="A-76/Z-8/B-56/Z-14">
<item>
<list>
<p name="A"></p>
<p name="B"></p>
</list>
</item>
</parent>
<parent name="A-45/Z-ty/B-9/C-14">
<item>
<list>
<p name="A"></p>
<p name="B"></p>
</list>
</item>
</parent>'''


soup = BeautifulSoup(txt, 'html.parser')

values = ["Z-99","Z-98","Z-97"]

r = re.compile(r'Z-\d+$')
for parent, new_val in zip(soup.find_all('parent', {'name': r}), values):
    parent['name'] = r.sub(new_val, parent['name'])

print(soup.prettify())

Prints:印刷:

<parent name="A-6/Z-9/B-64/Z-99">
 <item>
  <list>
   <p name="A">
   </p>
   <p name="B">
   </p>
  </list>
 </item>
</parent>
<parent name="A1/Zh/B/C-12">
 <item>
  <list>
   <p name="A">
   </p>
   <p name="B">
   </p>
  </list>
 </item>
</parent>
<parent name="AS-5/ZD-9/B-67/Z-98">
 <item>
  <list>
   <p name="A">
   </p>
   <p name="B">
   </p>
  </list>
 </item>
</parent>
<parent name="An4/Zd-8/B-5/C-13">
 <item>
  <list>
   <p name="A">
   </p>
   <p name="B">
   </p>
  </list>
 </item>
</parent>
<parent name="A-76/Z-8/B-56/Z-97">
 <item>
  <list>
   <p name="A">
   </p>
   <p name="B">
   </p>
  </list>
 </item>
</parent>
<parent name="A-45/Z-ty/B-9/C-14">
 <item>
  <list>
   <p name="A">
   </p>
   <p name="B">
   </p>
  </list>
 </item>
</parent>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM