``! /usr/bin/env python
coding=utf-8
'''
Created on 2013-3-31
将info_fight_monster.xls转换为XML格式。转换分为以下几步:
step1:读取xml文件,info_fight_monster.xls
step2:解析出怪物基本信息,在base这个sheet里面,并放到一个xml文件中
step3:解析出怪物的技能信息,并将技能信息插入到对应的xml中
step4:解析出怪物的奖励信息,并插入到对应XML中
step5:保存xml文件
@author:tylerzhu'''
fromlxmlimportetree
importxlrd3, codecs
attrib = ["monsterSID","classId","type","level","attrib",#
"attack","skill","define","speed","strength",#
"luck","name","desc","icon","url","frameRate"];
prize = ["monsterSID","prizeSID","type","infoType","gameID","itemType",#
"itemSID","num","plusSID","level","name","desc","icon"];
skill = ["monsterSID","skillSID","skillLevel"];
info_fight_monster_xml = etree.ElementTree(etree.Element("data"));
defopenxls():
excel = xlrd3.open_workbook("xls/info_fight_monster.xls");
base = excel.sheet_by_name("base");
monster = excel.sheet_by_name("skill");
prize = excel.sheet_by_name("prize");
return(base, monster, prize);
defbuildMonsterBase(xls):
sheet = xls[0];
forrowinrange(1, sheet.nrows):
monsterAttr = {};
forcolinrange(0, sheet.ncols):
ifsheet.cell(row, col).value !=Noneandcol < len(attrib):
monsterAttr[attrib[col]] = sheet.cell(row, col).value;
iftype(monsterAttr[attrib[col]]) == float:
print(str(round(monsterAttr[attrib[col]])))
monsterAttr[attrib[col]] = str(round(monsterAttr[attrib[col]]));
sub = etree.SubElement(info_fight_monster_xml.getroot(),"monster", monsterAttr);
sub.tail ="\n"
input()
defbuildMonsterSkill(xls):
sheet = xls[1];
forrowinrange(1, sheet.nrows):
skillAttr = {};
forcolinrange(1, sheet.ncols):
ifsheet.cell(row, col).value !=Noneandcol < len(attrib):
skillAttr[skill[col]] = sheet.cell(row, col).value;
iftype(skillAttr[skill[col]]) == float:
skillAttr[skill[col]] = str(round(skillAttr[skill[col]]));
monsterSID = str(int(sheet.cell(row,0).value));
monster = info_fight_monster_xml.find("*[@monsterSID='"+ monsterSID +"']");
monster.text = ("\n\t");
sub = etree.SubElement(monster,"skill", skillAttr);
sub.tail ="\n\t"
defbuildMonsterPrize(xls):
sheet = xls[2];
forrowinrange(1, sheet.nrows):
prizeAttr = {};
forcolinrange(1, sheet.ncols):
ifsheet.cell(row, col).value !=Noneandcol < len(attrib):
prizeAttr[prize[col]] = sheet.cell(row, col).value;
iftype(prizeAttr[prize[col]]) == float:
prizeAttr[prize[col]] = str(round(prizeAttr[prize[col]]));
monsterSID = str(int(sheet.cell(row,0).value));
monster = info_fight_monster_xml.find("*[@monsterSID='"+ monsterSID +"']");
monster.text = ("\n\t");
sub = etree.SubElement(monster,"prize", prizeAttr);
sub.tail ="\n\t"
defbuild():
xls = openxls();
buildMonsterBase(xls);
buildMonsterSkill(xls);
buildMonsterPrize(xls);
输出合并之后的配置
ouput = codecs.open('output/info_fight_monster.xml','w','utf-8');
ouput.write(etree.tounicode(info_fight_monster_xml.getroot()))
ouput.close();
if__name__ =='__main__':
build();``
网友评论