tool_transform_to_utf8.py
#!/usr/bin/python
# encoding:utf-8
r'''
遍历working/android/assets/下的csv文件,全部转换成utf-8格式
'''
import codecs
import os
import shutil
import sys
from pip._vendor.requests.packages import chardet
import tool_env
import tool_file_util
import tool_log_util
__author__ = 'andrew'
def list_all_files(root):
all_paths = tool_file_util.list_files_with_filter(root, ".csv", True)
return all_paths
def to_utf8(file_paths):
hint = "to_utf8"
default_encode = "gb18030"
default_py_encode = "utf-8"
for src_path in file_paths:
csv_file = None
encoding_type = None
new_path = None
try:
csv_file = open(src_path, "r+")
old_text = csv_file.read()
code_inform = chardet.detect(old_text)
encoding_type = code_inform["encoding"]
if (encoding_type.lower() != "utf-8"):
window_encondings = ["gbk", "gb2312"]
lower = encoding_type.lower()
count = window_encondings.count(lower)
if (count > 0):
tool_log_util.verb(