• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            專職C++

            不能停止的腳步

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              163 Posts :: 7 Stories :: 135 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(28)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

                在游戲開發過程中,游戲配置更新是很常的事情。其中一個主要的方法,是用excel來進行游戲配表。然后導出,可以導出前臺和后臺的配置,這樣策劃就不用針對前后臺,分別給配置了。在這里,python就有大作用了。通過以前的積累,這里給出一個目前我這里使用的最新的,希望能給大家有所幫助。
            我這里配置,支持.conf(ini格式),.csv,.jsn和.mall或.js(json格式),.cfg(我這里定義的一種數據格式和對應的ActionScript代碼),還有.sql和.key格式。并支持最新的excel 2013的表格。(這個是由第三方庫提供的)。
                首先,在excel表格中,要有一個表名tablelist,這個表格用來描述導出哪些配置。這個表的主要的字段有“表名,說明,輸出文件名,輸出的字段,輸出的類名,查詢的Key”,其中是應用于.cfg格式。
            表名:指對應的excel表格的名稱。
            說明:描述這個導出配置是做什么用的。
            輸出文件名:輸出的配置文件名。
            輸出的字段:輸出excel中的表格中字段
            輸出的類名:如果是.cfg格式的數據,這里需要提供輸出ActionScript類的名稱,其他格式無視
            查詢的Key:如果是.cfg格式的數據,這里可以定義一組查詢的Key字段,這樣在ActionScript中,會生成一個查詢的函數
                其次:excel一個有用的公式:=IF(F2<>"",CONCATENATE("[",VLOOKUP(F2,Props!A:B,2,FALSE),",",G2,"]"),"[0,0]"),通過VLOOKUP查詢Props表的A到B列,輸出第二列。這樣策劃只要要查詢的名稱不后,后面對應的數據,變化,整個配置表的數據,會做同步變化,如下圖

                  這里使用的第三方python庫是xlrd,下載地址是# http://pypi.python.org/pypi/xlrd
                  在python源文件的開頭,要加上
            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            不然寫代碼中有中文的時候,就會有錯誤!
            下面給出一個實際的tablelist截圖:

            下面給出一個等級表的例子截圖:

            這樣以后,就可以用了。下面,這里給出完整的python代碼
            #!/usr/bin/python
            #
             -*- coding: utf-8 -*-
            #
             這段代碼主要的功能是把excel表格轉換成utf-8格式的json文件
            #
             lastdate:2011-8-15 14:21 version 1.1 

            import os
            import sys
            import codecs
            import xlrd  # http://pypi.python.org/pypi/xlrd

            RefClassList = []
            strPackageName = u"com.hxgd.cfg";

            def FloatToString (aFloat):
                if type(aFloat) != float:
                    return ""
                strTemp = str(aFloat)
                strList = strTemp.split(".")
                if len(strList) == 1 :
                    return strTemp
                else:
                    if strList[1] == "0" :
                        return strList[0]
                    else:
                        return strTemp

            # 查找第1個非要求的字符串的下標
            def findFirstNot(paramString, paramBegin, paramSubstr):
                for i in range(paramBegin, len(paramString)):
                    if paramSubstr.find(paramString[i]) == -1:
                        return i
                return -1

            # 解析filter字符串,返回變量數組
            def parseFilterKey(paramFilter):
                ret = []
                begin = 0
                while True:
                    index = paramFilter.find("$", begin)
                    if index >= 0:
                        index += 1
                        end = findFirstNot(paramFilter, index, "1234567890abcdefghijklmnopqrstuvwxyz_ABC DEFGHIJKLMNOPQRSTUVWXYZ")
                        key = paramFilter[index:end]
                        ret.append(key)
                        begin = end
                    else:
                        return ret

            # 讀入字段映射表
            def readMap(paramTable):
                mapTable = {}
                nrow = paramTable.nrows
                if paramTable.ncols == 0:
                    return mapTable

                for r in range(nrow):
                    k = paramTable.cell_value(r, 0)
                    if paramTable.ncols < 2:
                        v = k
                    else:
                        v = paramTable.cell_value(r, 1)
                        if (len(v) == 0):
                            v = k
                    mapTable[k] = v
                return mapTable

            # 讀取字段列表
            def readFieldMap(paramFields):
                mapField = {}
                strList = paramFields.split(",")
                for f in strList:
                    mapField[f] = f
                return mapField

            #            table2as3config(destTable, destFileName, mapTable, mapParam)

            def CellToString(paramCell):
                strCellValue = u""
                if type(paramCell) == unicode:
                    strCellValue = paramCell
                elif type(paramCell) == float:
                    strCellValue = FloatToString(paramCell)
                else:
                    strCellValue = str(paramCell)
                return strCellValue

            def table2as3config(paramTable, paramDestFileName, paramUseFields, paramClassName, paramKeyMap):
                nrows = paramTable.nrows
                ncols = paramTable.ncols
                col_index_list = range(ncols)

                title_map_to_index = {}
                field_flag = [["", False, 0] for i in col_index_list]
                
                use_field_index = []        

                for index in col_index_list:
                    field_name = paramTable.cell_value(0, index)
                    strTitle = field_name.replace(u"\n", u"").replace(u"\"", u"")  # 取出引號和\n這樣的字符
                    field_flag[index][0] = strTitle
                    if (field_name.rfind(u"\"") >= 0):
                        field_flag[index][1] = True
                        field_flag[index][2] = 1
                    title_map_to_index[strTitle] = index  # 得到字段名對應的下標

                for use_field in paramUseFields:
                    if use_field in title_map_to_index:
                        use_field_index.append(title_map_to_index[use_field])
                # 生成類名
                strClassName = u"[" + paramClassName + u"]\r\n";
                # 生成字段列表
                strFieldList = u"<";
                field_count = len(use_field_index)
                if field_count > 0:
                    for i in range(field_count - 1):
                        strFieldList += field_flag[use_field_index[i]][0]
                        strFieldList += ","
                    strFieldList += field_flag[use_field_index[field_count - 1]][0]
                strFieldList += ">\r\n"

                # 輸出配置
                objDir = os.path.dirname(paramDestFileName)
                if objDir and not os.path.exists(objDir):
                    os.makedirs(objDir)    
                f = codecs.open(paramDestFileName, "w""utf-8")
                f.write(strClassName)
                f.write(strFieldList)

                for i in range(field_count):
                    col_index = use_field_index[i]
                    strCell = CellToString(paramTable.cell_value(1, col_index))
                    print (u"[1,", i, u"]=", strCell)
                    if len(strCell) > 0:
                        chFirst = strCell[0]
                        if chFirst == u'[':
                            field_flag[col_index][2] = 2
                
                for r in range(1, nrows):
                    strTmp = u"#"
                    if field_count < 1:
                        continue
                    for i in range(field_count - 1):
                        col_index = use_field_index[i]
                        strCell = CellToString(paramTable.cell_value(r, col_index))
                        if field_flag[col_index][1]:
                            strTmp += u"\"" + strCell + u"\""
                        else:
                            strTmp += strCell
                        strTmp += u","
            #            if r == 1:
            #
                            print (u"[", r,u",",i, u"]=",strCell)
            #
                            chFirst = strCell[0]
            #
                            if chFirst == u'[':
            #
                                field_flag[col_index][2]=2
                    col_index = use_field_index[field_count - 1]
                    strCell = CellToString(paramTable.cell_value(r, col_index))
                    if field_flag[col_index][1]:
                        strTmp += u"\"" + strCell + u"\""
                    else:
                        strTmp += str(strCell)
                    strTmp += u"\r\n"
                    f.write(strTmp);
                f.close()
                # 生成類
                
                
                strClassName = paramClassName
                RefClassList[len(RefClassList):] = [strClassName]
                as3 = codecs.open("./" + strClassName + u".as""w""utf-8")
                as3.write(u"package " + strPackageName + u"\n")
                as3.write(u"{\n")
                as3.write(u"    public class " + strClassName + u" extends ConfigBase\n")
                as3.write(u"    {\n")
                as3.write(u"        public function " + strClassName + u"()\n")
                as3.write(u"        {\n")
                as3.write(u"            super();\n")
                as3.write(u"        }\n\n")
                as3.write(u"        public static var List:Array = [];\n\n")
                as3.write(u"        public static function AddRecord(paramRecord:Array):void\n")
                as3.write(u"        {\n")
                as3.write(u"            var r:" + strClassName + u" = new " + strClassName + u"();\n")
                as3.write(u"            r.DoLoad(paramRecord);\n")
                as3.write(u"            List.push(r);\n")
                as3.write(u"        }\n\n")
                print("paramKeyMap:", paramKeyMap)
                for map_key in paramKeyMap:
                    print("map_key:" + map_key)
                    as3.write(u"        public static function GetBy_" + map_key + "(paramKey:*):" + strClassName + "\n")
                    as3.write(u"        {\n")
                    as3.write(u"            for each (var record:" + strClassName + " in List)\n")
                    as3.write(u"            {\n")
                    as3.write(u"                if (record." + map_key + " == paramKey)\n")
                    as3.write(u"                {\n")
                    as3.write(u"                    return record;\n")
                    as3.write(u"                }\n")
                    as3.write(u"            }\n")
                    as3.write(u"            return null;\n")
                    as3.write(u"        }\n\n")

                as3.write(u"        override public function DoLoad(paramRecord:Array):void\n        {\n")
                for i in range(field_count):
                    col_index = use_field_index[i]
                    strFieldName = field_flag[col_index][0]
                    as3.write(u"            " + strFieldName + u" = paramRecord[" + str(i) + u"];\n")
                as3.write(u"        }\n\n")

                for i in range(field_count):
                    col_index = use_field_index[i]
                    strFieldName = field_flag[col_index][0]
                    iFlag = field_flag[col_index][2]
                    if iFlag == 0:
                        as3.write(u"        public var " + strFieldName + u":Number;\n")
                    elif iFlag == 1:
                        as3.write(u"        public var " + strFieldName + u":String;\n")
                    elif iFlag == 2:
                        as3.write(u"        public var " + strFieldName + u":Array=[];\n")
                as3.write(u"    }\n}\n")
                as3.close();
                print "Create ", paramDestFileName, " OK"
                return



            def table2jsn(table, jsonfilename, mapTable, mapParam):
            # {"name":"數組名", "var":"變量名", "index1":True, "filter":"$Online>0 and $Name=='加速錦囊(1小時)'"}
                nrows = table.nrows
                ncols = table.ncols
                hasMap = (len(mapTable) > 0)
                objDir = os.path.dirname(jsonfilename)
                if objDir and not os.path.exists(objDir):
                    os.makedirs(objDir)    
                f = codecs.open(jsonfilename, "w""utf-8")
                strTmp = u""
                
                # 解析filter字符串
                filterKey = []
                filterString = ""
                if "filter" in mapParam and len(mapParam["filter"]) > 0:
                    filterString = mapParam["filter"].decode("utf8")
                    filterKey = parseFilterKey(filterString)

                # var xxx = 
                if ("var" in mapParam) and (len(mapParam["var"]) > 0):
                    strTmp += u"var " + mapParam["var"] + u" = "

                # name:[
                if "name" in mapParam:
                    if (len(mapParam["name"]) > 0):
                        strTmp += u"{\n\t\"" + mapParam["name"] + "\":"
                else:
                    strTmp += u"{\n\t\"list\":"

                if len(strTmp) == 0:  # 此時加個\t使前后對齊
                    strTmp += u"\t[\n"
                else:
                    strTmp += u"[\n"

                if ("index1" in mapParam) and (mapParam["index1"]):
                    strTmp += u"\t\t{},\n"
                f.write(strTmp)
                rs = 0
                for r in range(1, nrows):
                    strTmp = u"\t\t{ "
                    i = 0
                    strFilter = filterString
                    skip_row = False
                    get_this = not (len(filterKey) > 0)
                    for c in range(ncols):
                        title = table.cell_value(0, c)
                        isString = (title.rfind(u"\"") >= 0)
                        title = title.replace(u"\n", u"").replace(u"\"", u"")

                        if hasMap:
                            if not title in mapTable:
                                continue
                            else:
                                title = mapTable[title]

                        strCellValue = u""
                        CellObj = table.cell_value(r, c)
                        if type(CellObj) == unicode:
                            strCellValue = CellObj
                        elif type(CellObj) == float:
                            strCellValue = FloatToString(CellObj)
                        else:
                            strCellValue = str(CellObj)

                        if isString:
                            strCellValue = strCellValue.replace(u"\n", u"").replace(u"\"", u"")

                        if not get_this and title in filterKey:
                            if isString:
                                strFilter = strFilter.replace(u"$" + title, u"\"" + strCellValue + u"\"")
                            else:
                                strFilter = strFilter.replace(u"$" + title, strCellValue)

                            if strFilter.find("$") == -1:
                                if not eval(strFilter):  # 被過濾了
                                    skip_row = True
                                    break
                                else:
                                    get_this = True  # 確定了這行要

                        if i > 0:
                            delm = u""
                        else:
                            delm = u""

                        if isString:
                            strTmp += delm + u"\"" + title + u"\":\"" + strCellValue + u"\""
                        else:
                            strTmp += delm + u"\"" + title + u"\":" + strCellValue
                        i += 1
                    
                    if skip_row:  # 被過濾了
                        continue
                    
                    strTmp += u" }"
                    if rs > 0:  # 不是第1行
                        f.write(u",\n")
                    f.write(strTmp)
                    rs += 1

                strTmp = u"\n\t]"
                if "name" in mapParam:
                    if (len(mapParam["name"]) > 0):
                        strTmp += u"\n}"
                else:
                    strTmp += u"\n}"

                strTmp += u"\n"
                f.write(strTmp)
                f.close()
                print "Create ", jsonfilename, " OK"
                return


            def table2mall(table, jsonfilename, mapTable, mapParam):
            # {"name":"數組名", "var":"變量名", "index1":True, "filter":"$Online>0 and $Name=='加速錦囊(1小時)'"}
                nrows = table.nrows
                ncols = table.ncols
                hasMap = (len(mapTable) > 0)
                objDir = os.path.dirname(jsonfilename)
                if objDir and not os.path.exists(objDir):
                    os.makedirs(objDir)    
                f = codecs.open(jsonfilename, "w""utf-8")
                strTmp = u""
                
                # 解析filter字符串
                filterKey = []
                filterString = ""
                if "filter" in mapParam and len(mapParam["filter"]) > 0:
                    filterString = mapParam["filter"].decode("utf8")
                    filterKey = parseFilterKey(filterString)

                # var xxx = 
                if ("var" in mapParam) and (len(mapParam["var"]) > 0):
                    strTmp += u"var " + mapParam["var"] + u" = "

                # name:[
                if "name" in mapParam:
                    if (len(mapParam["name"]) > 0):
                        strTmp += u"{\n\t\"" + mapParam["name"] + "\":"
                else:
                    strTmp += u"{\n\t\"list\":"

                if len(strTmp) == 0:  # 此時加個\t使前后對齊
                    strTmp += u"\t[\n"
                else:
                    strTmp += u"[\n"

                if ("index1" in mapParam) and (mapParam["index1"]):
                    strTmp += u"\t\t{},\n"
                f.write(strTmp)
                rs = 0
                for r in range(1, nrows):
                    strTmp = u"\t\t{ "
                    i = 0
                    strFilter = filterString
                    skip_row = False
                    get_this = not (len(filterKey) > 0)
                    for c in range(ncols):
                        title = table.cell_value(0, c)
                        isString = (title.rfind(u"\"") >= 0)
                        title = title.replace(u"\n", u"").replace(u"\"", u"")

                        if hasMap:
                            if not title in mapTable:
                                continue
                            else:
                                title = mapTable[title]

                        strCellValue = u""
                        CellObj = table.cell_value(r, c)
                        if type(CellObj) == unicode:
                            strCellValue = CellObj
                        elif type(CellObj) == float:
                            strCellValue = FloatToString(CellObj)
                        else:
                            strCellValue = str(CellObj)

                        if isString:
                            strCellValue = strCellValue.replace(u"\n", u"").replace(u"\"", u"")

                        if not get_this and title in filterKey:
                            if isString:
                                strFilter = strFilter.replace(u"$" + title, u"\"" + strCellValue + u"\"")
                            else:
                                strFilter = strFilter.replace(u"$" + title, strCellValue)

                            if strFilter.find("$") == -1:
                                if not eval(strFilter):  # 被過濾了
                                    skip_row = True
                                    break
                                else:
                                    get_this = True  # 確定了這行要

                        if i > 0:
                            delm = u""
                        else:
                            delm = u""

                        if isString:
                            strTmp += delm + u"\"" + title + u"\":\"" + strCellValue + u"\""
                        else:
                            strTmp += delm + u"\"" + title + u"\":" + strCellValue
                        i += 1
                    
                    if skip_row:  # 被過濾了
                        continue
                    
                    strTmp += u" }"
                    if rs > 0:  # 不是第1行
                        f.write(u",\n")
                    f.write(strTmp)
                    rs += 1

                strTmp = u"\n\t]"
                if "name" in mapParam:
                    if (len(mapParam["name"]) > 0):
                        strTmp += u"\n}"
                else:
                    strTmp += u"\n}"

                strTmp += u"\n"
                f.write(strTmp)
                f.close()
                print "Create ", jsonfilename, " OK"
                return

            def table2sql(table, jsonfilename, mapTable, mapParam):
            # {"name":"表名", "delete":False, "commit":True, "filter":"$Online>0 and $Name=='加速錦囊(1小時)'"}
                nrows = table.nrows
                ncols = table.ncols
                hasMap = (len(mapTable) > 0)
                objDir = os.path.dirname(jsonfilename)
                if objDir and not os.path.exists(objDir):
                    os.makedirs(objDir)

                # 解析filter字符串
                filterKey = []
                filterString = ""
                if "filter" in mapParam and len(mapParam["filter"]) > 0:
                    filterString = mapParam["filter"].decode("utf8")
                    filterKey = parseFilterKey(filterString)

                tablename = destFileName[:destFileName.rfind(".")]  # 用文件名做表名
                tablename = tablename[tablename.rfind("\\") + 1:]
                if ("name" in mapParam) and len(mapParam["name"]) > 0:
                    tablename = mapParam["name"]

                f = codecs.open(jsonfilename, "w""utf-8")
                if not ("delete" in mapParam and not mapParam["delete"]):
                    f.write(u"truncate table " + tablename + u";\n")
                f.write(u"set names 'utf8';\n")

                if not(("commit" in mapParam) and not mapParam["commit"]):
                    f.write(u"set autocommit=0;\n")

                for r in range(1, nrows):
                    strTmp = u"insert into " + tablename + " set "
                    i = 0
                    strFilter = filterString
                    skip_row = False
                    get_this = not (len(filterKey) > 0)
                    for c in range(ncols):
                        title = table.cell_value(0, c)
                        isString = (title.rfind(u"\"") >= 0)
                        title = title.replace(u"\n", u"").replace(u"\"", u"")

                        if hasMap:
                            if not title in mapTable:
                                continue
                            else:
                                title = mapTable[title]

                        strCellValue = u""
                        CellObj = table.cell_value(r, c)
                        if type(CellObj) == unicode:
                            strCellValue = CellObj
                        elif type(CellObj) == float:
                            strCellValue = FloatToString(CellObj)
                        else:
                            strCellValue = str(CellObj)
                        
                        if isString:
                            strCellValue = strCellValue.replace(u"\n", u"").replace(u"'", u"\"")

                        if not get_this and title in filterKey:
                            if isString:
                                strFilter = strFilter.replace(u"$" + title, u"\"" + strCellValue + u"\"")
                            else:
                                strFilter = strFilter.replace(u"$" + title, strCellValue)

                            if strFilter.find("$") == -1:
                                if not eval(strFilter):  # 被過濾了
                                    skip_row = True
                                    break
                                else:
                                    get_this = True  # 確定了這行要

                        if i > 0:
                            delm = u""
                        else:
                            delm = u""

                        if isString:
                            strTmp += delm + title + u" = '" + strCellValue + u"'"
                        else:
                            strTmp += delm + title + u" = " + strCellValue
                        i += 1

                    if skip_row:  # 被過濾了
                        continue

                    strTmp += u";\n"
                    f.write(strTmp)

                if not(("commit" in mapParam) and not mapParam["commit"]):
                    f.write(u"commit;\n")
                f.write(u"\n")
                f.close()
                print "Create ", jsonfilename, " OK"
                return

            def table2ini(table, inifilename, mapTable, mapParam):
            # {"name":"section名", "filter":"$Online>0 and $Name=='加速錦囊(1小時)'"}
                nrows = table.nrows
                ncols = table.ncols
                hasMap = (len(mapTable) > 0)
                objDir = os.path.dirname(inifilename)
                if objDir and not os.path.exists(objDir):
                    os.makedirs(objDir)
                    
                # 解析filter字符串
                filterKey = []
                filterString = ""
                if "filter" in mapParam and len(mapParam["filter"]) > 0:
                    filterString = mapParam["filter"].decode("utf8")
                    filterKey = parseFilterKey(filterString)

                section = destFileName[:destFileName.rfind(".")]  # 用文件名做節名
                section = section[section.rfind("\\") + 1:]
                if ("name" in mapParam) and len(mapParam["name"]) > 0:
                    section = mapParam["name"]

                f = codecs.open(inifilename, "w""utf-8")
                rs = 1
                for r in range(1, nrows):
                    strTmp = u"[" + section + str(rs) + u"]\n"
                    strFilter = filterString
                    skip_row = False
                    get_this = not (len(filterKey) > 0)
                    
                    for c in range(ncols):
                        title = table.cell_value(0, c)
                        isString = (title.rfind(u"\"") >= 0)
                        title = title.replace(u"\n", u"").replace(u"\"", u"")

                        if hasMap:
                            if not title in mapTable:
                                continue
                            else:
                                title = mapTable[title]

                        strCellValue = u""
                        CellObj = table.cell_value(r, c)
                        if type(CellObj) == unicode:
                            strCellValue = CellObj
                        elif type(CellObj) == float:
                            strCellValue = FloatToString(CellObj)
                        else:
                            strCellValue = str(CellObj)

                        strCellValue = strCellValue.replace(u"\n", u"")  # 去掉換行
                        if not get_this and title in filterKey:
                            if isString:
                                strFilter = strFilter.replace(u"$" + title, u"\"" + strCellValue.replace(u"\"", u"") + u"\"")
                            else:
                                strFilter = strFilter.replace(u"$" + title, strCellValue)

                            if strFilter.find("$") == -1:
                                if not eval(strFilter):  # 被過濾了
                                    skip_row = True
                                    break
                                else:
                                    get_this = True  # 確定了這行要

                        strTmp += title + u" = " + strCellValue + "\n"

                    if skip_row:  # 被過濾了
                        continue

                    rs += 1
                    strTmp += u"\n"
                    f.write(strTmp)

                strTmp = u"[" + section + u"]\n"
                strTmp += u"Count = " + str(rs - 1) + u"\n"
                f.write(strTmp)

                f.close()
                print "Create ", inifilename, " OK"
                return

            def table2csv(table, csvfilename, mapTable, mapParam):
            # {"title":False, "filter":"$Online>0 and $Name=='加速錦囊(1小時)'"}
                nrows = table.nrows
                ncols = table.ncols
                hasMap = (len(mapTable) > 0)
                objDir = os.path.dirname(csvfilename)
                if objDir and not os.path.exists(objDir):
                    os.makedirs(objDir)
                f = codecs.open(csvfilename, "w""utf-8")
                
                # 解析filter字符串
                filterKey = []
                filterString = ""
                if "filter" in mapParam and len(mapParam["filter"]) > 0:
                    filterString = mapParam["filter"].decode("utf8")
                    filterKey = parseFilterKey(filterString)

                for r in range(nrows):
                    i = 0
                    strFilter = filterString
                    skip_row = False
                    get_this = not (len(filterKey) > 0)
                    strTmp = u""

                    if r == 0 and ("title" in mapParam) and not (mapParam["title"]):
                        print("#########################################")
                        continue

                    for c in range(ncols):
                        title = table.cell_value(0, c)
                        isString = (title.rfind(u"\"") >= 0)
                        title = title.replace(u"\n", u"").replace(u"\"", u"").replace(u",", u"")

                        if hasMap:
                            if not title in mapTable:
                                continue
                            else:
                                title = mapTable[title]

                        if i > 0:
                            delm = u","
                        else:
                            delm = u""

                        if r == 0:  # 第一行不同
                            strTmp += delm + title
                        else:
                            strCellValue = u""
                            CellObj = table.cell_value(r, c)
                            if type(CellObj) == unicode:
                                strCellValue = CellObj.replace(u"\n", u"")  # .replace(u",", u"")
                            elif type(CellObj) == float:
                                strCellValue = FloatToString(CellObj)
                            else:
                                strCellValue = str(CellObj).replace(u"\n", u"").replace(u",", u"")

                            if not get_this and title in filterKey:
                                if isString:
                                    strFilter = strFilter.replace(u"$" + title, u"\"" + strCellValue.replace(u"\"", u"") + u"\"")
                                else:
                                    strFilter = strFilter.replace(u"$" + title, strCellValue)

                                if strFilter.find("$") == -1:
                                    if not eval(strFilter):  # 被過濾了
                                        skip_row = True
                                        break
                                    else:
                                        get_this = True  # 確定了這行要

                            strTmp += delm + strCellValue
                        i += 1

                    if skip_row:  # 被過濾了
                        continue

                    strTmp += u"\n"
                    f.write(strTmp)
                f.close()
                print "Create ", csvfilename, " OK"
                return

            #   RefClassList[1:] = [strClassName]
            #
                as3 = codecs.open("./"+strClassName+u".as","w","utf-8")
            #
                as3.write(u"package " + strPackageName + u"\n")

            def create_ref():
                as3 = codecs.open(u"./ClassRef.as""w""utf-8")
                as3.write(u"package " + strPackageName + u"\n")
                as3.write(u"{\n")
                as3.write(u"    public class ClassRef\n")
                as3.write(u"    {\n")
                as3.write(u"        public function ClassRef()\n")
                as3.write(u"        {\n")
                for c in RefClassList:
                    as3.write(u"            new " + c + u"();\n")
                as3.write(u"        }\n")
                as3.write(u"    }\n")
                as3.write(u"}\n")
                as3.write(u"\n")
                return

            def table2key(table, jsonfilename, mapTable, mapParam):
            # {"var":"變量名", "filter":"$Online>0 and $Name=='加速錦囊(1小時)'"}
                iRows = table.nrows
                # iCols = table.ncols
                hasMap = (len(mapTable) > 0)
                objDir = os.path.dirname(jsonfilename)
                if objDir and not os.path.exists(objDir):
                    os.makedirs(objDir)    
                f = codecs.open(jsonfilename, "w""utf-8")
                strTmp = u""

                # 解析filter字符串
                filterKey = []
                filterString = ""
                if "filter" in mapParam and len(mapParam["filter"]) > 0:
                    filterString = mapParam["filter"].decode("utf8")
                    filterKey = parseFilterKey(filterString)

                # var xxx = 
                if ("var" in mapParam) and (len(mapParam["var"]) > 0):
                    strTmp += u"var " + mapParam["var"] + u" = "

                strTmp += u"{\n"
                f.write(strTmp)
                rs = 0
                for r in range(1, iRows):  # 跳過第1行標題
                    strTmp = u"\t"
                    i = 0
                    strFilter = filterString
                    skip_row = False
                    get_this = not (len(filterKey) > 0)
                    for c in range(2):  # 只處理最前面2列
                        title = table.cell_value(0, c)
                        isString = (title.rfind(u"\"") >= 0)
                        title = title.replace(u"\n", u"").replace(u"\"", u"")

                        if hasMap:
                            if not title in mapTable:
                                continue
                            else:
                                title = mapTable[title]

                        strCellValue = u""
                        CellObj = table.cell_value(r, c)
                        if type(CellObj) == unicode:
                            strCellValue = CellObj
                        elif type(CellObj) == float:
                            strCellValue = FloatToString(CellObj)
                        else:
                            strCellValue = str(CellObj)

                        if isString:
                            strCellValue = strCellValue.replace(u"\n", u"").replace(u"\"", u"")

                        if not get_this and title in filterKey:
                            if isString:
                                strFilter = strFilter.replace(u"$" + title, u"\"" + strCellValue + u"\"")
                            else:
                                strFilter = strFilter.replace(u"$" + title, strCellValue)

                            if strFilter.find("$") == -1:
                                if not eval(strFilter):  # 被過濾了
                                    skip_row = True
                                    break
                                else:
                                    get_this = True  # 確定了這行要

                        if i > 0:
                            delm = u":"
                        else:
                            delm = u""

                        if isString:
                            strTmp += delm + u"\"" + strCellValue + u"\""
                        else:
                            strTmp += delm + strCellValue
                        i += 1

                    if skip_row:  # 被過濾了
                        continue
                    
                    if rs > 0:  # 不是第1行
                        f.write(u",\n")
                    f.write(strTmp)
                    rs += 1

                strTmp = u"\n}\n"
                f.write(strTmp)
                f.close()
                print "Create ", jsonfilename, " OK"
                return

            if __name__ == '__main__':
                if len(sys.argv) < 2:
                    print 'Usage: %s <excel_file>' % sys.argv[0]
                    sys.exit(1)

                print "handle file: %s" % sys.argv[1]

                excelFileName = sys.argv[1]
                data = xlrd.open_workbook(excelFileName)
                table = data.sheet_by_name(u"tablelist")
                rs = table.nrows
                for r in range(rs - 1):
                    destTableName = table.cell_value(r + 1, 0)
                    destFileName = table.cell_value(r + 1, 2)
                    s = "undefined"
                    KeyMapParam = {}
                    strUseFields = u""
                    strKeyFields = u""
                    strClassName = u""

                    if (table.ncols >= 4):
                        strUseFields = table.cell_value(r + 1, 3)

                    if (table.ncols >= 5):
                        strClassName = table.cell_value(r + 1, 4)

                    if (table.ncols >= 6):
                        strKeyFields = table.cell_value(r + 1, 5)

                    stFieldList = strUseFields.split(",")  # 有用的字段列表

                    mapParam = {}

                    print "\nCreate " + destTableName + " ==> " + destFileName + " Starting"

                    destTable = data.sheet_by_name(destTableName)
                    mapTable = readFieldMap(strUseFields)
                    KeyMapParam = readFieldMap(strKeyFields)

                    suffix = destFileName[destFileName.rfind("."):].lower()
                    if suffix == ".csv":
                        table2csv(destTable, destFileName, mapTable, mapParam)
                    elif suffix == ".jsn" or suffix == ".js":
                        table2jsn(destTable, destFileName, mapTable, mapParam)
                    elif suffix == ".mall":
                        table2mall(destTable, destFileName, mapTable, mapParam)
                    elif suffix == ".conf":
                        table2ini(destTable, destFileName, mapTable, mapParam)
                    elif suffix == ".sql":
                        table2sql(destTable, destFileName, mapTable, mapParam)
                    elif suffix == ".key":
                        table2key(destTable, destFileName, mapTable, mapParam)
                    elif suffix == ".cfg":
                        table2as3config(destTable, destFileName, stFieldList, strClassName, KeyMapParam)
                    else:
                        print "only support jsn, ini, csv, conf, sql,.mall, .key, .cfg format"
                        exit(1)
                    create_ref()
                print "All OK"
            posted on 2014-02-08 17:19 冬瓜 閱讀(2922) 評論(1)  編輯 收藏 引用 所屬分類: 原創python

            Feedback

            # re: 用python導出excel中的配置 2014-10-21 14:35 createdream
            可不可以指導我一下怎么使用這個python腳本,最近也在為寫配置而煩惱,我的qq:364642404  回復  更多評論
              

            久久国产精品视频| 三级片免费观看久久| 久久久亚洲裙底偷窥综合 | 久久婷婷五月综合色99啪ak| 久久美女人爽女人爽| 97r久久精品国产99国产精| 麻豆AV一区二区三区久久| 中文字幕乱码久久午夜| 久久精品国产免费观看三人同眠| 色8激情欧美成人久久综合电| 久久久久这里只有精品| 精品久久人人做人人爽综合 | 久久这里有精品视频| 久久久久国产亚洲AV麻豆| 久久天天日天天操综合伊人av| 久久婷婷五月综合成人D啪 | 99精品国产综合久久久久五月天 | 青青青伊人色综合久久| 国产69精品久久久久9999| 国产日韩久久久精品影院首页| 国产精品美女久久久久AV福利| 精品久久久久久无码中文字幕 | 久久久久无码精品国产不卡| 久久国产精品一国产精品金尊| 久久婷婷综合中文字幕| 久久男人中文字幕资源站| 久久天天躁夜夜躁狠狠躁2022| 日韩精品久久久久久免费| 狠狠色丁香久久婷婷综| 国内精品久久久久久久涩爱| 亚洲精品乱码久久久久久蜜桃| 久久亚洲精品中文字幕| 99久久国产热无码精品免费久久久久| 久久国产视频网| 久久青青草原亚洲av无码app| 久久久久综合网久久| 欧美亚洲国产精品久久久久| 久久久婷婷五月亚洲97号色| 99久久免费国产精品| 欧美日韩精品久久久久| 97久久超碰国产精品2021|