示例xml内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<peach> <DataModel name="test"> <String value="select" mutable="false" /> <String value=" " mutable="false" /> <String value="id" /> <String value="," mutable="false" /> <String value="name" /> <String value=" " mutable="false" /> <String value="from" mutable="false" /> <String value=" " mutable="false" /> <String value="table_for_test" /> </DataModel> </peach> |
如上这段内容,其实是peach数据模型的一段定义,今天通过脚本模拟自动建模,生成的文档含有大量的mutable="false"的内容。根据peach的描述,这一属性的意义可以让peach忽略对其进行mute操作(实际好像还是会mute),为了简洁起见,减少建模过程中的费脑,直接将数据模型定义文件中重复出现的mutab=“false”内容进行合并。如下是操作的方式,实在想不到好的方法,直接按照c的数组操作方式来强行遍历:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from xml.etree import cElementTree as ET xml = ET.parse('Sample.xml') root = xml.getroot() i = 0 while i < len(root[0])-1: print(len(root[0])) print(root[0][i].tag, root[0][i].attrib) if 'mutable' in root[0][i].attrib.keys() and root[0][i].attrib['mutable'].lower() == 'false': if 'mutable' in root[0][i+1].attrib.keys() and root[0][i+1].attrib['mutable'].lower() == 'false': root[0][i + 1].attrib['value'] = root[0][i].attrib['value'] + root[0][i + 1].attrib['value'] del root[0][i] i = 0 continue i += 1 xml.write('newSample.xml') |
修改后新生成的newSample.xmp文件内容变化为:
1 2 3 4 5 6 7 8 9 10 11 12 |
<peach> <DataModel name="test"> <String mutable="false" value="select " /> <String value="id" /> <String mutable="false" value="," /> <String value="name" /> <String mutable="false" value=" from " /> <String value="table_for_test" /> </DataModel> </peach> |
看起来像是需要的格式,明天找一些负载的模型在试试。
I do accept as true with all the ideas you’ve introduced in your post.
They’re really convincing and will definitely work. Nonetheless, the posts
are too short for starters. May you please lengthen them a little from next time?
Thank you for the post. https://mobaqq.com