Python parsing GBK format XML file

Source: Internet
Author: User
Tags python script

XML file

<?xml version="1.0"encoding="GBK"? ><records serial_number="1"> <record id="1"Name="Admin"password="Admin"Auth="1"Email=""Receivedmail="0"description="System Super Administrator, cannot be deleted. "Createtime="N /A"/> </records>

Python script

#coding =utf-8Import xml.dom.minidom//format conversion xml_file_string=open ('/nasgui/share/user.xml','R'). Read () xml_file_string=xml_file_string.replace ('<?xml version= "1.0" encoding= "GBK"?>','<?xml version= "1.0" encoding= "Utf-8"?>') xml_file_string=unicode (xml_file_string,encoding='GBK'). Encode ('Utf-8');
Dom=xml.dom.minidom.parseString (xml_file_string)//Get root node root=dom.documentelement
Get Records itemlist=root.getelementsbytagname ('Record')
Item=itemlist[0]un=item.getattribute ('name') print UN

Python parsing GBK format XML file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.