本文目錄一覽:
- 1、python3編碼轉換
- 2、怎樣將xlsx文件用Python腳本轉成csv文件編碼為UTF-8
- 3、怎麼通過Python將一個目錄下的所有txt文件全部從ASCII編碼轉換為UTF-8(或Unicode)編碼
python3編碼轉換
python3 \u53cc\u7684(雙的)直接變成中文啊,你其他哪裡出問題了吧
怎樣將xlsx文件用Python腳本轉成csv文件編碼為UTF-8
策劃的配置表為 xlsx 表格,可以有注釋,公式。
伺服器和客戶端使用的配置文件需要轉成 csv 文件。
使用 WPS 另存無法批量轉換,並且結果不是utf8編碼的,還需要用Notepad++轉編碼。
除了 xlsx 轉為 csv, 其他格式文件保持原樣,如 *.ini, *.xml, *.lua.
server/ 子目錄特殊處理,不能複製到客戶端。
用python腳本實現,依賴 openpyxl 庫。
#!/usr/bin/env python
# coding: utf-8
# datatab.py
# 從策劃配置表目錄 game\Design\配置表\」
# 生成伺服器的 game\Program\server\six\datatab\」 目錄,
# 和客戶端的 game\Program\client\Assets\Config\」 目錄。
# 所有xlsx文件生成csv文件,其他文件原樣複製。
# 其中 server\ 目錄特殊處理,僅對伺服器有效,客戶端跳過。
#
# 依賴openpyxl庫:
# 參考代碼
# 測試環境:Python3.4
# Usage: datatab.py game dir
# Example: datatab.py “d:\game”
# game dir 是根目錄,包含Design/, Program/ 目錄。
from openpyxl import Workbook
from openpyxl.compat import range
from openpyxl.cell import get_column_letter
from openpyxl import load_workbook
import csv
import os
import sys
import shutil
def xlsx2csv(filename):
# try:
xlsx_file_reader = load_workbook(filename = filename, data_only = True)
for sheet in xlsx_file_reader.get_sheet_names():
# 僅第1個sheet輸出到一個csv文件中,文件名後綴替換為.csv
csv_filename = os.path.splitext(filename)[0] + ‘.csv’
csv_file = open(csv_filename, ‘w’, encoding=’utf8′, newline=”)
csv_file_writer = csv.writer(csv_file)
sheet_ranges = xlsx_file_reader[sheet]
for row in sheet_ranges.rows:
row_container = []
for cell in row:
row_container.append(cell.value)
csv_file_writer.writerow(row_container)
# End of for row.
csv_file.close()
break # 僅輸出第1個sheet
# End of for sheet.
# End of try.
# except Exception as e:
# print(e)
# End of xlsx2csv().
def datatab_convert(game_dir):
”’從 game\Design\配置表\ 輸出到
game\Program\server\six\datatab\
game\Program\client\Assets\Config\
”’
design_dir = os.path.join(game_dir, ‘Design/配置表/’)
server_dir = os.path.join(game_dir, ‘Program/server/six/datatab/’)
client_dir = os.path.join(game_dir, ‘Program/client/Assets/Config/’)
# 刪除舊文件。
print(“Delete ” + server_dir)
try:
shutil.rmtree(server_dir)
except:
pass
print(“Delete ” + client_dir)
try:
shutil.rmtree(client_dir)
except:
pass
# 生成server文件
print(“Creating ” + server_dir)
shutil.copytree(design_dir, server_dir)
files = get_files(server_dir)
convert_files(files)
# 複製client文件
print(“Copy ” + client_dir)
shutil.copytree(server_dir, client_dir)
shutil.rmtree(os.path.join(client_dir, ‘server/’))
print(“Done. Total files: %d” % len(files))
# End of datatab_convert().
def get_files(dir):
”’Get a list of files under input dir.”’
result = []
for root,dirs,files in os.walk(dir):
for f in files:
result.append(os.path.join(root, f))
return result
# End of get_files().
def convert_files(files):
”’轉換一批文件.
files 是列表,元素為完整路徑名。
”’
for f in files:
ext = os.path.splitext(f)[1].lower()
if ‘.xlsx’ != ext:
print(f + ” – keep”)
continue
print(f + ” – csv”)
xlsx2csv(f)
os.remove(f)
# End of convert_files().
if __name__ == ‘__main__’:
if len(sys.argv) != 2:
print(‘usage: datatab game dir’)
else:
datatab_convert(sys.argv[1])
sys.exit(0)
# Usage: datatab.py game dir
# Example: datatab.py “d:\game”
# game dir 是SVN根目錄,包含Design/, Program/ 目錄。
為方便使用,將datatab.py 打包成 exe, 這樣不能安裝Python就能運行。
下載並安裝Python3, 安裝openpyxl包,保證本地可以運行 datatab.py.
下載並安裝PyInstaller:
pip install pyinstaller
運行
pyinstaller –onefile datatab.py
ImportError: No module named ‘jdcal’
可能openpyxl安裝時自帶的jdcal無法找到,刪除
C:\Python34\Lib\site-packages\jdcal-1.0-py3.4.egg
重新安裝:pip install jdcal
怎麼通過Python將一個目錄下的所有txt文件全部從ASCII編碼轉換為UTF-8(或Unicode)編碼
記事本的另存就有這功能吧?
編碼方式預設的是ANSI,可以選擇Unicode/Unicode big endian/UTF-8
原創文章,作者:LHVMT,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/128889.html