python 替换xml中的一行或者两行
有一个xml文件<!--Theblockmapwhichconsistsofelementswhichmayeitherbearangeofblocksorasingle...
有一个xml文件
<!-- The block map which consists of elements which may either be a
range of blocks or a single block. The 'chksum' attribute
(if present) is the checksum of this blocks range. -->
<BlockMap>
<Range> 0-229956 </Range>
<Range> 229960-229961 </Range>
<Range> 230976-230980 </Range>
<Range> 231936 </Range>
<Range> 232000-232001 </Range>
<Range> 236032 </Range>
<Range> 240128 </Range>
<Range> 244224 </Range>
<Range> 248320 </Range>
<Range> 258560-259588 </Range>
<Range> 262656 </Range>
<Range> 262658-262660 </Range>
<Range> 262664 </Range>
<Range> 263680 </Range>
<Range> 264704 </Range>
<Range> 281088 </Range>
<Range> 285184 </Range>
<Range> 294896-295295 </Range>
<Range> 295297 </Range>
<Range> 295299-295301 </Range>
<Range> 295303 </Range>
<Range> 295309-295310 </Range>
<Range> 295326 </Range>
<Range> 303326-303331 </Range>
<Range> 327936-327937 </Range>
<Range> 393472-393473 </Range>
<Range> 459008-459009 </Range>
<Range> 491776-499976 </Range>
<Range> 524544-524545 </Range>
<Range> 590080-590081 </Range>
<Range> 807152-807493 </Range>
<Range> 807497 </Range>
<Range> 808517-808520 </Range>
<Range> 809472 </Range>
<Range> 809537-809588 </Range>
<Range> 809984-810688 </Range>
<Range> 811008-813568 </Range>
<Range> 814080-817664 </Range>
<Range> 818176-821760 </Range>
<Range> 822272-825856 </Range>
<Range> 825921-825924 </Range>
<Range> 826368-827148 </Range>
给定两个值A和B,就是要跳过A和B 之间的区域,加入0<A<B<229956,则应该修改成
<Range> 0-(A-1) </Range>
<Range> B-229956 </Range>
先要判断A和B 所在的区域,再修改,有大神知道么,谢谢!对python不是很了解,望大神指点! 展开
<!-- The block map which consists of elements which may either be a
range of blocks or a single block. The 'chksum' attribute
(if present) is the checksum of this blocks range. -->
<BlockMap>
<Range> 0-229956 </Range>
<Range> 229960-229961 </Range>
<Range> 230976-230980 </Range>
<Range> 231936 </Range>
<Range> 232000-232001 </Range>
<Range> 236032 </Range>
<Range> 240128 </Range>
<Range> 244224 </Range>
<Range> 248320 </Range>
<Range> 258560-259588 </Range>
<Range> 262656 </Range>
<Range> 262658-262660 </Range>
<Range> 262664 </Range>
<Range> 263680 </Range>
<Range> 264704 </Range>
<Range> 281088 </Range>
<Range> 285184 </Range>
<Range> 294896-295295 </Range>
<Range> 295297 </Range>
<Range> 295299-295301 </Range>
<Range> 295303 </Range>
<Range> 295309-295310 </Range>
<Range> 295326 </Range>
<Range> 303326-303331 </Range>
<Range> 327936-327937 </Range>
<Range> 393472-393473 </Range>
<Range> 459008-459009 </Range>
<Range> 491776-499976 </Range>
<Range> 524544-524545 </Range>
<Range> 590080-590081 </Range>
<Range> 807152-807493 </Range>
<Range> 807497 </Range>
<Range> 808517-808520 </Range>
<Range> 809472 </Range>
<Range> 809537-809588 </Range>
<Range> 809984-810688 </Range>
<Range> 811008-813568 </Range>
<Range> 814080-817664 </Range>
<Range> 818176-821760 </Range>
<Range> 822272-825856 </Range>
<Range> 825921-825924 </Range>
<Range> 826368-827148 </Range>
给定两个值A和B,就是要跳过A和B 之间的区域,加入0<A<B<229956,则应该修改成
<Range> 0-(A-1) </Range>
<Range> B-229956 </Range>
先要判断A和B 所在的区域,再修改,有大神知道么,谢谢!对python不是很了解,望大神指点! 展开
2个回答
展开全部
a='<Range> 0-229956 </Range>'
b="'<Range> 0-(A-1) </Range>'\n'<Range> B-229956 </Range>'"
d=open("a.xml","w")
while 1:
line=d.readline()
if line==a:
replace(a,b)
ifnotline:
break
##如果你的A,B两个值是从终端读取的话在开始read一下就行:
read A
read B
b="'<Range> 0-(A-1) </Range>'\n'<Range> B-229956 </Range>'"
d=open("a.xml","w")
while 1:
line=d.readline()
if line==a:
replace(a,b)
ifnotline:
break
##如果你的A,B两个值是从终端读取的话在开始read一下就行:
read A
read B
更多追问追答
追问
a=' 0-229956 '
b="' 0-(A-1) '\n' B-229956 '"这个过程可以通过python实现么,替换的位置需要通过判断确定
追答
对啊,我上面的不就是Python实现吗。循环里不是判断吗。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询