python用e替換xml內容的簡單介紹

  • 1、python 怎麼修改xml文件內容
  • 2、python 替換xml中的一行或者兩行
  • 3、求大神指教:如何用python讀取xml文件中指定標籤的文檔內容並將其修改?最好寫下詳細代碼,非常感謝哈!
  • 4、Python的XML節點替換或字符串替換問題,此為補償貼,只限luotuo512來回答
  • 5、Python的XML節點替換或字符串替換問題

類似於:

#!/usr/bin/evn python 

  #coding:utf-8 

   

  try: 

    import xml.etree.cElementTree as ET 

  except ImportError: 

    import xml.etree.ElementTree as ET 

  import sys 

   

  try: 

    tree = ET.parse(“country.xml”)     #打開xml文檔 

    #root = ET.fromstring(country_string) #從字符串傳遞xml 

    root = tree.getroot()         #獲得root節點  

  except Exception, e: 

    print “Error:cannot parse file:country.xml.” 

    sys.exit(1) 

  print root.tag, “—“, root.attrib  

  for child in root: 

    print child.tag, “—“, child.attrib 

   

  print “*”*10 

  print root[0][1].text   #通過下標訪問 

  print root[0].tag, root[0].text 

  print “*”*10 

   

  for country in root.findall(‘country’): #找到root節點下的所有country節點 

    rank = country.find(‘rank’).text   #子節點下節點rank的值 

    name = country.get(‘name’)      #子節點下屬性name的值 

    print name, rank 

      

  #修改xml文件 

  for country in root.findall(‘country’): 

    rank = int(country.find(‘rank’).text) 

    if rank  50: 

      root.remove(country) 

   

  tree.write(‘output.xml’)

給你推薦一篇文章看看吧:

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

使用python自帶的ElementTree模塊,給你個例子你就知道了

xml文檔

?xml version=”1.0″ encoding=”utf-8″?

config

id0/id

log_pathE:/Python/log_path

/config

Python 代碼,修改id節的內容

from xml.etree import ElementTree

xml_file=’config.xml’

xml=ElementTree.ElementTree(file=xml_file).getroot()

xml.find(‘id’).text=1

兩種方法:

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()

結果:

roottext

a1a1for a/a1

a2a2for a/a2

a3a3for a/a3

root

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()

lz是好人。

fpr=open(“text.txt”)

data=fpr.read()

n=0

for i in range(len(data)):

if data[i]==’/’ and data[i+1]==”:

n+=1

newdata=data[0:i]+str(n)+’a’+str(n)+’fora/a’+str(n)+”

k=i

break

for i in range(k+1,len(data)):

if data[i]==’/’ and data[i+1]==”:

n+=1

newdata=newdata+data[i-2:i]+str(n)+’a’+str(n)+’fora/a’+str(n)+”

newdata+=’root’

fpr.close()

fpw=open(“newtext.txt”,”w+”)

fpw.write(newdata)

fpw.close()

測試數據:roottexta/a/a/root

輸出結果:roottexta1a1fora/a1a2a2fora/a2a3a3fora/a3root

原創文章,作者:E8A1P,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/126143.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
E8A1P的頭像E8A1P
上一篇 2024-10-03 23:06
下一篇 2024-10-03 23:06

相關推薦

  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29

發表回復

登錄後才能評論