2009-12-17 5 views
1

XML 파일에서 요소의 속성을 편집하고 싶습니다. 내가 값을 대체 텍스트 파일에서 다른 값과 속성을 결과 할 XML 편집 속성

<Parameter name="Spec 2 Circumference/Length" type="real" mode="both"> 
    <Value>0.0</Value> 
    <Result>0.0</Result> 
</Parameter> 

처럼

파일이 보인다.

좋습니다. 미리 감사드립니다.

+0

은 0.0 0.0 유효한 XML입니까? 더 많은 파일을 게시하십시오 .. – miku

답변

1

ElementTree을 사용한 예. Value 요소 텍스트를 일부 문자열로 바꿉니다. Result 요소의 절차는 아날로그이며 여기에서 생략됩니다.

#!/usr/bin/env python 

xml = """ 
<Parameter name="Spec 2 Circumference/Length" type="real" mode="both"> 
    <Value>0.0</Value> 
    <Result>0.0</Result> 
</Parameter> 
""" 

from elementtree.ElementTree import fromstring, tostring 

# read XML, here we read it from a String 
doc = fromstring(xml) 

for e in doc.findall('Value'): 
    e.text = 'insert your string from your textfile here!' 

print tostring(doc) 

# will result in: 
# 
# <Parameter mode="both" name="Spec 2 Circumference/Length" type="real"> 
#  <Value>insert your string from your textfile here!</Value> 
#  <Result>0.0</Result> 
# </Parameter> 
+0

제안에 감사드립니다. 실제로 이러한 종류의 요소가 반복적으로 포함 된 XML 파일이 있습니다. XMl 파일에서 줄을 읽고 원하는대로 처리하고 싶습니다. 힌트를주세요. 답장을 보내 주셔서 감사합니다. – manoj1123

+0

코멘트에서 새로운 질문을하는 대신 위의 질문을 수정하십시오. 더 이해하기 쉽습니다. 또한 항상 코드/데이터의 관련 부분을 질문에 포함하고 붙임 위치 및 이유 (오류, 예외 사항 등)에 대한 힌트를 제공하십시오. - 마지막 참고 사항 : 더 많은 사람들 (rep-hunters)이 다음 질문에 답할 것입니다. 그것은 '커뮤니티 위키'가 아니 었습니다 – miku

+0

그리고 SO;)에 오신 것을 환영합니다. – miku