分类 Python 下的文章

最近項目是台灣這邊的, 但使用到的是之前大陸製作的source, 一直沒處理, 這邊製作了 python 進行簡體轉繁體處理

import os
import glob
import zhconv

root_path = r"g:/OlgCase/bbm/source/Web/"
subdir = ["application"]

def getSubFileonFolder(path):
    count = 0
    for path, subdirs, files in os.walk(path):
        for name in files:
            fn = os.path.join(path, name)
            if fn.endswith(".js"):
                print('[js]fn:' + fn)
                count+=1
                testWriteFile(fn)
            elif fn.endswith(".php"):
                print('[php]fn:' + fn)
                count+=1
                testWriteFile(fn)

    print("檔案數",count)

def testWriteFile(path):
    fn = path
    f = open(path, mode='r')
    content = f.read()
    f.close()
    content2 = zhconv.convert(content, 'zh-tw')

    with open(fn, "w", encoding="UTF-8", newline='\n') as file:
        file.write(content2)
        file.close()

def process():
    for sub in subdir:
        getSubFileonFolder(root_path + sub)

if __name__ == "__main__":
    process()

$ pip install opencv-python

出現錯誤, python setup.py egg_info
23546-nulflha4fhj.png

需要升级 pip 版本
$ pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple/

安裝失敗, 後來去官網下載安裝檔升級

安裝 opencv 老是失敗, 應該是被牆了, 但掛了vpn還是無效

$ pip install opencv-python -i https://mirrors.aliyun.com/pypi/simple/
50505-r297uv50pvb.png

直接到網上下載檔案安裝, windows 選擇 win_amd64 格式
https://pypi.tuna.tsinghua.edu.cn/simple/opencv-python/

安裝檔案
$ pip install opencv_python-4.5.5.64-cp36-abi3-win_amd64.whl

查看安裝清單
$ pip list
04921-kiurv32sr2o.png

由於電腦轉換了編碼為 utf 發現 vs 打開後不少檔案出現轉換提示, 可能是原本開發時直接在 vs 中新增 .cs 文件的緣故, 因為不透過 unity 新增 .cs 文件估計是參考系統編碼, 所以不少文件編碼都是 ascii 格式, 為了解決這個問題弄了個批量轉換工具, 大致如下吧

需要先安裝 chardet 工具

$ pip install chardet 

代碼如下:

import os
import chardet

root_path = r"g:\OlgCase\bbm\source\Unity\Assets\Hotfix"
count = 0
for path, subdirs, files in os.walk(root_path):
for name in files:
    if not name.endswith('cs'):
        continue
    fn = os.path.join(path, name)

    print(fn)
    f = open(fn, mode='rb')
    content = f.read()
    f.close()
    result = chardet.detect(content)

    if result['encoding'] == None:
        print('error')
        continue

    print(result)
    if (result['encoding'] != 'UTF-8-SIG' and
     result['encoding'] != 'UTF-8' and 
     result['encoding'] != 'utf-8'):

        print('process, encoding:' + result['encoding'] + ", fn:" + fn)
        if result["encoding"] == "GB2312":
            result["encoding"] = "GBK"
        with open(fn, "w", encoding="UTF-8-SIG", newline='\n') as file:
            line = str(content, encoding = "GBK")
            print(line)
            file.write(line)
            count +=1
            file.close()

print("轉換檔案數",count)

之前為了參考github的轟炸超人AI, 發現source的盤面跟設計的剛好相反,於是用了python做了轉換

原本的盤面數據, 轉換如下

a = [
[0,0,0,0,2,0,2,0,0,0,0,0,1,0,1,0,2,0],
[0,0,0,1,0,1,2,1,2,0,1,0,0,2,0,0,0,1],
[2,0,2,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,1,2,1,2,2,0,0,0,0,1,0,1,0,1,1],
[2,0,2,0,2,0,2,0,0,1,0,0,0,2,0,2,0,0],
[0,1,0,1,0,1,0,1,0,1,2,2,1,0,1,2,1,0],
[2,0,2,0,2,2,2,0,2,0,0,0,2,2,0,2,0,2],
[0,1,0,1,0,1,0,0,0,0,2,2,0,2,0,2,0,0],
[0,0,0,0,0,2,0,0,2,2,0,0,0,0,0,0,0,0]
]

b = [[0 for _ in range(len(a))] for _ in range(len(a[0]))]
for j in range(len(a)):
    for i in range(len(a[0])):
        b[i][j] = a[j][i]

59494-7vx73wa8xbg.png