我想修改此文件,以便从1000开始后BOOKSEQNUM
文本将增加10,即第一个节点文本为1010
,然后1020
依此类推:
<root>
<BOOKSEQNUM DOCID="HKLAC_V14.0002.para0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0003.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0004.para0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0005.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0006.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0016.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0022.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0031.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0041.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0046.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0047.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0058.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0063.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0070.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0078.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0084.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0087.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0088.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0093.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0100.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0107.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0117.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0120.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0123.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0129.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0139.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0146.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0161.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0169.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0180.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0189.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0202.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0212.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0218.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0219.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0220.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0236.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0241.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0250.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0260.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0267.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0272.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0281.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0290.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0293.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0298.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0303.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0319.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0328.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0336.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0343.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0357.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0363.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0369.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0378.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0382.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0388.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0392.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0393.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0401.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0414.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0424.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0435.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0442.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0447.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0459.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0465.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0473.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0477.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0481.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0492.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0496.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0499.para0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0500.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0501.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0504.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0505.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0512.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0518.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0524.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0531.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0542.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0547.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0548.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0559.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0576.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0577.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0584.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0602.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0609.subpara0"></BOOKSEQNUM>
<BOOKSEQNUM DOCID="HKLAC_V14.0617.subpara0"></BOOKSEQNUM>
</root>
这是我编写的python脚本,如何写入同一个文档?
from lxml import etree
myXML = r"C:/Users/DalalNS/Desktop/splitingthecontent/HK_Chunking_Scripts/HKEFP/BookSeq_HKEFP_Aviation.xml"
tree = etree.parse(myXML)
root = tree.getroot()
code = root.xpath('//root/BOOKSEQNUM')
count=1000
Root = etree.Element('root')
doc = etree.ElementTree(Root)
for i in code:
print(i.attrib)
count=count+10
i.text=str(count)
print(i.text)
答案 0 :(得分:1)
from lxml import etree
filename = r'path/to/your/file.xml' # change this
with open(filename, 'r') as f:
tree = etree.parse(f)
start = 1000
for node in tree.findall('./'):
node.text = str(start + 10)
start += 10
with open(filename, 'wb') as f:
tree.write(f)
注意:写入必须采用二进制模式,否则您将获得TypeError
。