#!/usr/bin/python # -*- coding: utf-8 -*- import xml.dom.minidom import os f = open("/home/a.txt") # 返回一个文件对象 line = f.readline() # 调用文件的 readline()方法 while line: print line # 后面跟 ',' 将忽略换行符 line = f.readline() f.close() str = "k1=0002,k2=0001,k3=0003" str1 = "" str2 = "" dict = {'':""} list1 = str.split(',') for i in list1: str1 = i.split('=')[0] str2 = i.split('=')[1] dict[str1] = str2 print(dict) #encoding:utf-8 ''' 根据一个给定的XML Schema,使用DOM树的形式从空白文件生成一个XML。 ''' from xml.dom.minidom import Document doc = Document() #创建DOM文档对象 DOCUMENT = doc.createElement('DOCUMENT') #创建根元素 DOCUMENT.setAttribute('content_method',"full")#设置命名空间 #DOCUMENT.setAttribute('xsi:noNamespaceSchemaLocation','DOCUMENT.xsd')#引用本地XML Schema doc.appendChild(DOCUMENT) ############item:Python处理XML之Minidom################ param= doc.createElement('param') param.setAttribute('key',"1") param.setAttribute('value',"1") DOCUMENT.appendChild(param) param= doc.createElement('param') param.setAttribute('key',"2") param.setAttribute('value',"2") DOCUMENT.appendChild(param) ########### 将DOM对象doc写入文件 f = open('/home/tel.xml','w') #f.write(doc.toprettyxml(indent = ' ', newl = ' ', encoding = 'utf-8')) doc.writexml(f,indent = ' ',newl = ' ', addindent = ' ',encoding='utf-8') f.close()