使用python將excel轉為lua文件
excel表格格式
說明:
1.前三行分別為:欄位中文解釋、欄位名、欄位類型
2.程序不用的欄位,加」_」前綴,不會生成進lua文件里
3.策劃填數值的時候,偶爾會遺漏數據,當存在空值時,依據欄位類型,填上默認值。
4.支持一個欄位填上多組數據,如」進階消耗」欄位,自定義類型」table」,代表{ {道具1id,道具1數量},{道具2id,道具2數量}}, … }
腳本文件目錄結構
windows bat命令,用於快速執行excel2lua.py腳本
excel2lua.py腳本代碼
import sys
import os
import xlrd
import re
# 當前腳本路徑
curpath = os.path.dirname(os.path.abspath(sys.argv[0]))
# 文件頭描述格式化文本
lua_file_head_format_desc = """--[[
%s
exported by excel2lua.py
from file:%s
--]]
"""
# 將數據導出到tgt_lua_path
def excel2lua(src_excel_path, tgt_lua_path):
print("[file] %s -> %s" % (src_excel_path, tgt_lua_path))
# load excel data
excel_data_src = xlrd.open_workbook(src_excel_path, encoding_override = "utf-8")
print("[excel] Worksheet name(s):", excel_data_src.sheet_names())
excel_sheet = excel_data_src.sheet_by_index(0)
print("[excel] parse sheet: %s (%d row, %d col)" % (excel_sheet.name, excel_sheet.nrows, excel_sheet.ncols))
# excel data dict
excel_data_dict = {}
# col name list
col_name_list = []
#col val type list
col_val_type_list = []
# ctype: 0 empty, 1 string, 2 number, 3 date, 4 boolean, 5 error
# 遍歷第二行的所有列 保存欄位名
for col in range(0, excel_sheet.ncols):
cell = excel_sheet.cell(1, col)
col_name_list.append(str(cell.value))
assert cell.ctype == 1, "found a invalid col name in col [%d] !~" % (col)
# 遍歷第三行的所有列 保存數據類型
for col in range(0, excel_sheet.ncols):
cell = excel_sheet.cell(2, col)
col_val_type_list.append(str(cell.value))
assert cell.ctype == 1, "found a invalid col val type in col [%d] !~" % (col)
# 剔除表頭、欄位名和欄位類型所在行 從第四行開始遍歷 構造行數據
for row in range(3, excel_sheet.nrows):
# 保存數據索引 默認第一列為id
cell_id = excel_sheet.cell(row, 0)
assert cell_id.ctype == 2, "found a invalid id in row [%d] !~" % (row)
# 檢查id的唯一性
if cell_id.value in excel_data_dict:
print("[warning] duplicated data id: "%d", all previous value will be ignored!~" % (cell_id.value))
# row data list
row_data_list = []
# 保存每一行的所有數據
for col in range(0, excel_sheet.ncols):
cell = excel_sheet.cell(row, col)
k = col_name_list[col]
cell_val_type = col_val_type_list[col]
# ignored the string that start with "_"
if str(k).startswith("_"):
continue
# 根據欄位類型去調整數值 如果為空值 依據欄位類型 填上默認值
if cell_val_type == "string":
if cell.ctype == 0:
v = """"
else:
v = ""%s"" % (cell.value)
elif cell_val_type == "int":
if cell.ctype == 0:
v = -1
else:
v = int(cell.value)
elif cell_val_type == "float":
if cell.ctype == 0:
v = -1
else:
v = float(cell.value)
elif cell_val_type == "table":
if cell.ctype == 0:
v = "{}"
else:
v = cell.value
else:
v = cell.value
# 加入列表
row_data_list.append([k, v])
# 保存id 和 row data
excel_data_dict[cell_id.value] = row_data_list
# 正則搜索lua文件名 不帶後綴 用作table的名稱 練習正則的使用
searchObj = re.search(r"([^\/:*?"<>|
]+).w+$", tgt_lua_path, re.M|re.I)
lua_table_name = searchObj.group(1)
# print("正則匹配:", lua_table_name, searchObj.group(), searchObj.groups())
# 這個就直接獲取文件名了
src_excel_file_name = os.path.basename(src_excel_path)
tgt_lua_file_name = os.path.basename(tgt_lua_path)
# file head desc
lua_file_head_desc = lua_file_head_format_desc % (tgt_lua_file_name, src_excel_file_name)
# export to lua file
lua_export_file = open(tgt_lua_path, "w")
lua_export_file.write(lua_file_head_desc)
lua_export_file.write("%s = {
" % lua_table_name)
# 遍歷excel數據字典 按格式寫入
for k, v in excel_data_dict.items():
lua_export_file.write(" [%d] = {
" % k)
for row_data in v:
lua_export_file.write(" {0} = {1},
".format(row_data[0], row_data[1]))
lua_export_file.write(" },
")
lua_export_file.write("}
")
lua_export_file.close()
print("[excel] %d row data exported!~" % (excel_sheet.nrows))
# Make a script both importable and executable (∩_∩)
if __name__ == "__main__":
if len(sys.argv) < 3:
print("python excel2lua.py <excel_input_path> <lua_output_path>")
exit(1)
excel2lua(os.path.join(curpath, sys.argv[1]), os.path.join(curpath, sys.argv[2]))
exit(0)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
執行bat命令,結果截圖
生成的lua文件截圖
檢測lua文件數據的有效性
※ubuntu16.04安裝TensorFlow的正確步驟
※Python LZW 演算法
TAG:程序員小新人學習 |