示例中主要思路:使用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
后续可能会写一个更通用的可视化的打表工具!