1. 程式人生 > >python操作xml,cvs,excel

python操作xml,cvs,excel

  • dom操作xml
import xml.dom.minidom as dom
import codecs

def loadDom(xml):
	return dom.parse(xml)

def load(xml):
	doc = dom.parse(xml)
	return doc.documentElement

def getElesByName(ele,tagName):
	return ele.getElementsByTagName(tagName);

def getEleByName(ele,tagName):
	eles = ele.getElementsByTagName(tagName)
	if len(eles) == 0:
		return None
	else:
		return ele.getElementsByTagName(tagName)[0];

def getChildText(ele,tagName):
	tags = tagName.split("|")
	for tag in tags:
		ele = getEleByName(ele,tag)
	if ele == None:
		return ""
	else:
		return getText(ele)

def getChild(ele,tagName):
	tags = tagName.split("|")
	for tag in tags:
		ele = getEleByName(ele,tag)
	if ele == None:
		return None
	else:
		return ele

def getChildren(ele,tagName):
	tags = tagName.split("|")

	for tag in tags[:len(tags)-1]:
		ele = getEleByName(ele,tag)
	if ele == None:
		return None
	else:
		return getElesByName(ele,tags[len(tags)-1])		

def getChildTextByNum(ele,tagName,num):
	tags = tagName.split("|")
	tagLen = len(tags)
	for i in range(tagLen-1):
		tag = tags[i]
		ele = getEleByName(ele,tag)
	eles = getElesByName(ele,tags[tagLen-1])
	if len(eles) <= num:
		return ""
	return getText(eles[num])


def getChildTextByAttr(ele,tagName,attrName,attrValue,childTagName):
	tags = tagName.split("|")
	tagLen = len(tags)
	for i in range(tagLen-1):
		tag = tags[i]
		ele = getEleByName(ele,tag)
	eles = getElesByName(ele,tags[tagLen-1])
	newEle = None
	for e in eles:
		if e.getAttribute(attrName) == attrValue:
			newEle = e
			break
	if newEle == None:
		return ""
	if childTagName == "":
		return getText(newEle)
	return	getChildText(newEle,childTagName)


def getChildByAttr(ele,tagName,attrName,attrValue):
	tags = tagName.split("|")
	tagLen = len(tags)
	for i in range(tagLen-1):
		tag = tags[i]
		ele = getEleByName(ele,tag)
	eles = getElesByName(ele,tags[tagLen-1])
	newEle = None
	for e in eles:
		if e.getAttribute(attrName) == attrValue:
			newEle = e
			break
	return newEle

def getText(ele):
	if len(ele.childNodes) == 0:
		return ""
	else:
		return ele.childNodes[0].data

def save(doc,fileName):
	f = codecs.open(fileName,"w+","utf-8")
	doc.writexml(f,indent='',addindent='',newl='',encoding='UTF-8')
	f.close()

def listToStr(li):
	str1 = ""
	for v in li:
		str1 = str1 + str(v) + "|"
	return str1

if __name__ == '__main__':
	print("xml test")


  • csv
import csv
import codecs


def load(csvFile):
	csvDict = {}
	f = codecs.open(csvFile,"r+","utf-8")
	lines = csv.reader(f)

	isFirstLine = True
	for line in lines:
		if isFirstLine:
			isFirstLine = False
			continue
		csvDict[line[0]] = line[1]
	f.close()
	return csvDict


if __name__ == '__main__':
	file = "soliders.csv"
	load(file)

  • openpyxl操作excel
import openpyxl


def load(fileName):
	return openpyxl.load_workbook(fileName)


def loadtodict(fileName,sheetName,keyCol,valueCols):
	xlDict={}
	wb = load(fileName)
	sheet = wb[sheetName]
	for row in range(2,sheet.max_row+1):
		if row == 0:
			continue
		valueList = []
		for col in valueCols:
			valueList.append(sheet[row][col].value)
		xlDict[sheet[row][keyCol].value] = valueList
	return xlDict


def listToStr(li):
	str = ""
	for v in li:
		str = str + v + "|"
	return str

def save(datas,file):
	wb = openpyxl.Workbook()
	sheet = wb["Sheet"]
	for row in datas:
		sheet.append(row)
	wb.save(file)


if __name__ == '__main__':
	datas = [["aa","bb","cc"],[1,2,3],[4,5,6]]
	save(datas,"D:/aa.xlsx")