unity打表工具——phython解析excel及加密解密

该博客介绍了如何使用Python的xlrd库读取Excel数据,并进行加密处理。通过遍历数据并异或操作实现加密,然后将加密后的数据写入文件。同时,文章提到了xlrd在高版本中无法解析xlsx文件的问题,解决方案是降级到1.2.0版本。此外,作者还计划开发一个可视化的打表工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 示例中主要思路:使用xlrd读取excel表数据,之后将数据和表名称按照utf-8编码转换成字节,遍历数据字节,与名称字节异或,写入文件,之后解密解析做相反处理即可。

Tips:示例中使用到了xlrd包,这配套的还有xlutils、xlwt,有需要可以自行下载;游戏固化数据,一般分为int数字、int类型列表、int类型字典、floa数字、float列表、float字典、string字符串、string列表、string字典,在打表的时候,将这些属性类型放在第一行,便于解析;

import xlrd
import os

class ReadExcelTool:

    def createFile(self, path):
        if not os.path.exists(path):
            os.makedirs(path)
        else:
            print("已存在该文件:{}".format(path))

    TypeList = ['', 'int', 'int+', 'int++', 'float', 'float+', 'float++', 'string',
                'string+', 'string++']

    configPath = "D:\\Projects\\learn_two\\Excel\\"
    datasPath = "D:\\Projects\\learn_two\\Datas\\"

    cellSeparator = '\t'
    lineSeparator = '\n'

    def readAll(self):
        self.createFile(self.datasPath)
        excelList = []
        for root, dirs, files in os.walk(self.configPath):
            for name in files:
                file = os.path.join(root, name)
                excelList.append(file)
                self.readExcel(file, name)

    def loadAll(self):
        for root, dirs, files in os.walk(self.datasPath):
            for name in files:
                file = os.path.join(root, name)
                self.loadData(file, name)

    def readExcel(self, path, name):
        wb = xlrd.open_workbook(path)
        sh1 = wb.sheet_by_index(0)

        content = ''

        for col in range(sh1.ncols):
            typeInt = self.GetDataType(sh1.cell_value(1, col))
            content += "{0}{1}".format(typeInt, self.cellSeparator)
        content += self.lineSeparator

        for row in range(2, sh1.nrows):
            for col in range(sh1.ncols):
                content += "{0}{1}".format(sh1.cell_value(row, col), self.cellSeparator)
            content += self.lineSeparator

        bytesStr = content.encode(encoding='utf-8')
        key = name.replace('.xls', '').encode(encoding='utf-8')

        result = []

        keyLen = len(key) - 1
        valueLen = len(bytesStr) - 1
        j = 0
        # print("keyLen:{0}, valueLen:{1}".format(keyLen, valueLen))
        for index in range(valueLen):
            if j == keyLen:
                j = 0
            # print("index:{0},j:{1}".format(index, j))
            num = bytesStr[index] ^ key[j]
            result.append(num)
            # print("{0}^{1}={2}".format(bytesStr[index], key[j], num))
            j += 1

        dataPath = os.path.join(self.datasPath, name.replace(".xls", ".bytes"))
        expertFile = open(dataPath, 'wb')

        print(result)
        resultByte = bytes(result)
        print(resultByte)

        expertFile.write(resultByte)
        expertFile.close()

    def loadData(self, path, name):
        file = open(path, 'rb')
        bytesStr = file.read()
        key = name.replace('.bytes', '').encode(encoding='utf-8')

        print("key:{}".format(key))
        print(bytesStr)

        result = []

        keyLen = len(key) - 1
        valueLen = len(bytesStr) - 1

        print("keyLen:{0}, valueLen:{1}".format(keyLen, valueLen))

        j = 0
        for index in range(valueLen):
            if j == keyLen:
                j = 0
            num = bytesStr[index] ^ key[j]
            result.append(num)
            # print("{0}^{1}={2}".format(bytesStr[index], key[j], num))

            j += 1

        print(result)
        resultBytes = bytes(result)
        print(resultBytes)
        asd = resultBytes.decode('utf-8')
        print(asd)

    def GetDataType(self, typeStr):
        temp = typeStr.lower
        index = self.TypeList.index(typeStr)
        if index >= 0:
            return index
        else:
            print('此类型{}貌似填写错误,请检查!'.format(typeStr))
            return -1

excel表结构示例:

说一下遇到的问题:xlrd高版本不能解析xlsx后缀的表格,会报错,只能解析xls后缀的。

解决办法:回退xlrd版本至1.2.0就可以了,怀疑是版权的问题导致高版本取消了解析该后缀文件的功能

命令行代码:

        pip show xlrd    看版本号的   

        pip uninstall xlrd  卸载当前的xlrd

        pip install xlrd==1.2.0  安装1.2版本的xlrd

后续可能会写一个更通用的可视化的打表工具!

                

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值