两个yaml转成的 excel对比

语言本地化过程中 ,yaml转成excel格式给翻译人员使用,翻译好之后再转换成yaml格式给游戏使用。但是在翻译人员编辑时是不可控的,有可能造成某种想不到的问题。因此,需要将翻译人员的excel转成yaml ,再将yaml转成excel和翻译人员的excel进行对比,看看有没有疏漏。对比的主要内容是键-值对比,python代码如下

 #coding=utf-8
__author__ = 'qzyn2320'

import sys
import yaml
import codecs

from yaml.constructor import Constructor

def add_bool(self, node):
    return self.construct_scalar(node)

Constructor.add_constructor(u'tag:yaml.org,2002:bool', add_bool)

reload(sys)
sys.setdefaultencoding('utf-8')
# excel文件读出存为dict
def ExackFromExcel(filename):
    import xlrd
    wb = xlrd.open_workbook(filename)
    sh = wb.sheet_by_index(0)
    excelDict = {}
    for line in xrange(1, sh.nrows):
        key = sh.cell_value(line, 0)
        value = sh.cell_value(line, 1)
        excelDict[key] = value
    return excelDict
#将对比两个dict
def CompareDict(baseDict, cmpDict):
    baseKeys = list(baseDict.keys())
    cnt = 0
    for key in baseKeys:
        if(cmpDict.has_key(key)):
            cnt += 1
            #打印键键相同,值不同的字典元素,打印后删除该键
            if(not (baseDict[key] == cmpDict[key])):
                print "value differ,key =  ", key, ",  value1 = " , baseDict[key], ", value2 = " ,cmpDict[key]
            del baseDict[key]
            del cmpDict[key]
    print "samekey num:", cnt
    #打印两个互不包含的键和值
    print "base extra :",baseDict
    print "cmp extra :", cmpDict
if __name__=="__main__":
    if len(sys.argv) < 3:
        sys.exit();
    basedict = ExackFromExcel(sys.argv[1])
    cmpdict = ExackFromExcel(sys.argv[2])
    CompareDict(basedict, cmpdict)
    sys.exit();

上一篇:内存规整部分源码学习笔记


下一篇:微博鸿蒙客户端回应偶发打字丢字等Bug:初步定位为系统问题