在百度知道里面有人问了这么一个问题:

有一个xml文件:
<root>text
<a/>
<a/>
...这里省略n个<a/> <root>
想替换成下面的形式:
<root>text
<a1>a1 for a</a1>
<a2>a2 for a</a2>
...n个<an>...</an> <root>
请问,应该怎么替换呢?用字符串替换或lxml包的方法都可以。谢谢!

我给出了两种答案,一种是用字符串替换,一种是xml解析。如下

字符串替换:

 old=open("test.xml")
lines=old.readlines()
a="<a/>"
i=1
newlines=[]
for line in lines:
if a in line:
line=line.replace(a,"<a"+str(i)+">a"+str(i)+"for a</a"+str(i)+">")
i=i+1
newlines.append(line) for line in newlines:
print line new=open("newtest.xml","w")
new.writelines(newlines)
new.close()
old.close()

  xml解析:

 import xml.dom.minidom
oldxmlfile=open("test.xml")
oldxml=oldxmlfile.read()
oldxmlfile.close()
doc = xml.dom.minidom.parseString(oldxml)
index=1
for node in doc.getElementsByTagName("a"):
node.tagName="a"+str(index)
index=index+1
newxml=doc.toprettyxml()
xmlfile=open("newxml2.xml","w")
xmlfile.write(newxml);
xmlfile.close()

python 修改xml文件的更多相关文章

  1. python处理xml文件

    参考:https://docs.python.org/2/library/xml.etree.elementtree.html 例子: <?xml version="1.0" ...

  2. Python实现XML文件解析

    1. XML简介 XML(eXtensible Markup Language)指可扩展标记语言,被设计用来传输和存储数据,已经日趋成为当前许多新生技术的核心,在不同的领域都有着不同的应用.它是web ...

  3. MyEclipse如何修改XML文件默认行宽

    1.MyEclipse如何修改XML文件默认行宽 Windows--->Preferences--->搜索xml--->XML--->XML Source--->Form ...

  4. MyEclipse如何修改XML文件默认打开的编辑器

    1.MyEclipse如何修改XML文件默认打开的编辑器 Windows--->Preferences--->General--->Editors--->File Associ ...

  5. Java 操纵XML之修改XML文件

    Java 操纵XML之修改XML文件 一.JAVA DOM PARSER DOM interfaces The DOM defines several Java interfaces. Here ar ...

  6. python读取xml文件报错ValueError: multi-byte encodings are not supported

    1.在使用python对xml文件进行读取时,提示ValueError: multi-byte encodings are not supported 很多贴子上说把xml的编码格式改为,就可以正常执 ...

  7. Python解析xml文件遇到的编码解析的问题

    使用python对xml文件进行解析的时候,假设xml文件的头文件是utf-8格式的编码,那么解析是ok的,但假设是其它格式将会出现例如以下异常: xml.parsers.expat.ExpatErr ...

  8. python读写xml文件

    python读取xml文件 xml文件是具有树状结构的,如果想要访问某个叶子结点,必须逐层获取其父结点,要读取某个叶子结点内容用text成员 使用前先加载xml工具包 try: import xml. ...

  9. python 解析 XML文件

    如下使用xml.etree.ElementTree模块来解析XML文件.ElementTree模块中提供了两个类用来完成这个目的: ElementTree表示整个XML文件(一个树形结构) Eleme ...

随机推荐

  1. 15.Subtree of Another Tree(判断一棵树是否为另一颗树的子树)

    Level:   Easy 题目描述: Given two non-empty binary trees s and t, check whether tree t has exactly the s ...

  2. cs231n学习笔记(二)图像分类

    图像分类可说是计算机视觉中的基础任务同时也是核心任务,做好分类可为检测,分割等高阶任务打好基础. 本节课主要讲了两个内容,K近邻和线性分类器,都是以猫的分类为例. 一. K近邻 以猫的分类为例,一张含 ...

  3. android:id 中区别。。

    一. android:id="@android:id/tabhost"   是调用系统内部的ID 和代码中 mTabContent = (FrameLayout) findView ...

  4. java坏境内存不够用 大量占用swap 临时加swap

    dd if=/dev/sda of=/tmp/mbr.bin   bs=512   count=1 查询2进制文件 file  看文件类型 思路 创建一个大文件作为swap 1.1创建文件 [root ...

  5. hive参数设置

    -- 设置hive的计算引擎为spark set hive.execution.engine=spark; -- 修复分区 set hive.msck.path.validation=ignore; ...

  6. mysql 示例数据库安装

    示例数据库不和bin安装文件在一块, 安装数据库没有这个选项 https://dev.mysql.com/doc/index-other.html

  7. github访问慢解决

    参考:https://github.com/chenxuhua/issues-blog/issues/3 hosts文件: # GitHub Start 192.30.253.112 github.c ...

  8. Python中的数据类型和数据结构

    一.数据类型 Python中有六个标准数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Sets(集合) Dictionary(字典) 其中,除列表Lis ...

  9. 转 Python 操作 MySQL 数据库

    #########http://www.runoob.com/python/python-mysql.html Python 标准数据库接口为 Python DB-API,Python DB-API为 ...

  10. 转 Celery 使用

    http://www.mamicode.com/info-detail-1798782.html https://blog.csdn.net/lu1005287365/article/details/ ...