本文实例讲述了python写xml文件的操作的方法,分享给大家供大家参考。具体方法如下:
要生成的xml文件格式如下:
<?xml version="1.0" ?> <!--Simple xml document__chapter 8--> <book> <title> sample xml thing </title> <author> <name> <first> ma </first> <last> xiaoju </last> </name> <affiliation> Springs Widgets, Inc. </affiliation> </author> <chapter number="1"> <title> First </title> <para> I think widgets are greate.You should buy lots of them forom <company> Spirngy Widgts, Inc </company> </para> </chapter> </book>
Python实现代码如下:
from xml.dom import minidom, Node doc = minidom.Document() doc.appendChild(doc.createComment("Simple xml document__chapter 8")) #generate the book book = doc.createElement('book') doc.appendChild(book) #the title title = doc.createElement('title') title.appendChild(doc.createTextNode("sample xml thing")) book.appendChild(title) #the author section author = doc.createElement("author") book.appendChild(author) name = doc.createElement('name') author.appendChild(name) firstname = doc.createElement('first') firstname.appendChild(doc.createTextNode("ma")) name.appendChild(firstname) lastname = doc.createElement('last') name.appendChild(lastname) lastname.appendChild(doc.createTextNode("xiaoju")) affiliation = doc.createElement("affiliation") affiliation.appendChild(doc.createTextNode("Springs Widgets, Inc.")) author.appendChild(affiliation) #The chapter chapter = doc.createElement('chapter') chapter.setAttribute('number', '1') title = doc.createElement('title') title.appendChild(doc.createTextNode("First")) chapter.appendChild(title) book.appendChild(chapter) para = doc.createElement('para') para.appendChild(doc.createTextNode("I think widgets are greate.\ You should buy lots of them forom")) company = doc.createElement('company') company.appendChild(doc.createTextNode("Spirngy Widgts, Inc")) para.appendChild(company) chapter.appendChild(para) print doc.toprettyxml()
希望本文所述对大家的Python程序设计有所帮助。

pythonlistsareimplementedesdynamicarrays, notlinkedlists.1) thearestoredIntIguousUousUousUousUousUousUousUousUousUousInSeripendExeDaccess, LeadingSpyTHOCESS, ImpactingEperformance

PythonoffersfourmainmethodstoremoveElementsfromalist : 1) 제거 (값) 제거 (값) removesthefirstoccurrencefavalue, 2) pop (index) 제거 elementatAspecifiedIndex, 3) delstatemeveselementsByindexorSlice, 4) RemovesAllestemsfromTheChmetho

Toresolvea "permissionDenied"오류가 발생할 때 오류가 발생합니다.

arraysarecrucialinpythonimageProcessingAstheyenableantureficient -manipulationand analysysofimagedata.1) ImagesAreconTortonumpyArrays, withGrayScaleImages2DarraysAndColorImagesS3darrays.2) arraysallowforvectorizedoperations, inablingastAdmentments bri

ArraysareSareSareStificerTanlistSforoperationsbenefitingfrom DirectMemoryAccessandfixed-sizestructures.1) AccessingElements : ArraysprovideConstant-timeaccessduetocontiguousUousUousSougues.2) 반복 : ArraysleAgeCachelocalityFasterItertion.3) Mem

ArraysareBetterForElement-WiseOperationsDuetOfasterAcccessandoptimizedimmentations.1) ArraysHaveCecontIguousMemoryFordirectAccess, 향상

Numpy에서 전체 배열의 수학적 작업은 벡터화 된 작업을 통해 효율적으로 구현 될 수 있습니다. 1) 추가 (ARR 2)와 같은 간단한 연산자를 사용하여 배열에서 작업을 수행하십시오. 2) Numpy는 기본 C 언어 라이브러리를 사용하여 컴퓨팅 속도를 향상시킵니다. 3) 곱셈, 분할 및 지수와 같은 복잡한 작업을 수행 할 수 있습니다. 4) 배열 모양이 호환되도록 방송 작업에주의를 기울이십시오. 5) NP.Sum ()과 같은 Numpy 함수를 사용하면 성능을 크게 향상시킬 수 있습니다.

Python에는 요소를 목록에 삽입하는 두 가지 주요 방법이 있습니다. 1) 삽입 (인덱스, 값) 메소드를 사용하여 지정된 인덱스에 요소를 삽입 할 수 있지만 큰 목록의 시작 부분에서 삽입하는 것은 비효율적입니다. 2) Append (value) 메소드를 사용하여 목록 끝에 요소를 추가하여 매우 효율적입니다. 대형 목록의 경우 Append ()를 사용하거나 Deque 또는 Numpy Array를 사용하여 성능을 최적화하는 것이 좋습니다.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

WebStorm Mac 버전
유용한 JavaScript 개발 도구

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

mPDF
mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.