簡介
- 由于項目中需要將國際化字符串導(dǎo)出成Excel表格胡野,所以就寫了這么一個腳本扔嵌,目前只實現(xiàn)了導(dǎo)出字符串案糙、后續(xù)會繼續(xù)實現(xiàn)將Excel合入到項目工程中去
-
該腳本是由Python實現(xiàn)的梧躺、主要用到的庫是openpyxl操作Excel表格奥吩、
from openpyxl import Workbook
import os
import re
wb = Workbook()
ws = wb.active
ws.title = 'iOSString'
ws.cell(row=1, column=1).value = 'key'
urlList = []
valueList = []
keyList = []
# savepath = input('請輸入需要保存的路徑:')
def __getfilename(path):
f_list = os.listdir(path)
for i in f_list:
if os.path.splitext(i)[1] == '.lproj':
url = path + '/' + i + '/' + 'Localizable.strings'
urlList.append(url)
valueList.append(os.path.splitext(i)[0])
# TODO:這里應(yīng)該要用線程控制 目前還不會
for i in f_list:
if i == 'Base.lproj':
url = path + '/' + i + '/' + 'Localizable.strings'
__dealbasefile(url)
# 處理key值 因為后面要遍歷查詢
def __dealbasefile(path):
with open(path) as f:
context = f.read()
partten = re.compile(r'"(.*)" = "(.*)";')
result = partten.findall(context)
for i in range(len(result)):
ws.cell(row=i + 2, column=1).value = result[i][0]
keyList.append(result[i][0])
for i in range(len(urlList)):
ws.cell(row=1, column=i + 2).value = valueList[i]
__dealExcel(urlList[i], i + 2)
- 該方法主要是用正則表達(dá)式檢測出key value
def __dealExcel(url, index):
print('當(dāng)前處理的文件是=',url)
with open(url) as f:
context = f.read()
partten = re.compile(r'"(.*)" = "(.*)";')
result = partten.findall(context)
resultDictionary = {}
for i in range(len(result)):
resultDictionary.setdefault(result[i][0], result[i][1])
for i in range(len(keyList)):
if keyList[i] in resultDictionary:
ws.cell(row=i + 2, column=index).value = resultDictionary[keyList[i]]
else:
print('字典里面沒有該key=',keyList[i])
# print(resultDictionary)
wb.save('/Users/tusm/Desktop/douban.xlsx')
- 主函數(shù) input 需要輸入項目中字符串的目錄
if __name__ == "__main__":
path = input('please input string path:')
__getfilename(path)
后面繼續(xù)完善