changetoutf-8
import chardet
import os
# ANSI文件转UTF-8
import codecs
import os def strJudgeCode(str):
return chardet.detect(str) def readFile(path): f = open(path, 'r',endoding='ANSI')
filecontent = f.read()
f.close() return filecontent def WriteFile(str, path):
try:
f = open(path, 'w')
f.write(str)
finally:
if f:
f.close() def converCode(path):
file_con = readFile(path)
result = strJudgeCode(file_con)
#print(file_con)
if result['encoding'] == 'utf-8':
#os.remove(path)
a_unicode = file_con.decode('utf-8')
gb2312 = a_unicode.encode('gbk')
WriteFile(gb2312, path) def listDirFile(dir):
list = os.listdir(dir)
for line in list:
print(line)
filepath = dir+line
print(filepath)
# if os.path.isdir(filepath):
# listDirFile(filepath)
# else:
# print(line)
converCode(filepath) if __name__ == '__main__': # listDirFile('./TRMD/') # 文件所在目录
file_path =r"C:\\Users\\Lenovo\\Desktop\\数据库设计\\爬虫脚本\\TRMD\\test"
files = os.listdir(file_path) for file in files:
file_name = file_path + '\\' + file
f = codecs.open(file_name, 'r','cp852')
ff = f.read()
file_object = codecs.open(file_path + '\\' + file, 'w', 'utf-8')
file_object.write(ff)
随机推荐
- 使用java代码执行linux命令
前提: java代码是在windows下面写的,要打包放到linux下面运行,并且执行某个脚本. java代码: try { // 起作用的代码其实就下面这一行, 参数是linux中要执行的代码 Ru ...
- TensorFlow入门-Tianic数据集训练
import pandas as pd import tensorflow as tf from sklearn.model_selection import train_test_split imp ...
- webserive学习记录4-获取天气的例子
学习到了如何创建使用webservice服务,下面就实际应用一下,从网络上获取天气数据. 先从网络上找到免费的webservice服务, 如这个网站:http://www.webxml.com.cn/ ...
- Apache Hive 执行HQL语句报错 ( 10G )
# 故障描述: hive > , ) as uuid, count(distinct(request_body["uuid"])) as count from log_bft ...
- git 拉取某个分支到本地
git 拉取其实只需要 git fetch origin xxx. git pull origin xxx即可
- servlet类
一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...
- vs2010+Aspx进行sharepoint2010工作流开发(3) 资料整理
http://www.cnblogs.com/janet/archive/2010/04/24/1719315.html http://www.cnblogs.com/poissonnotes/arc ...
- 结对项目3-bug的三种状态
这周和小伙伴结对构造程序,来深刻理解软件测试中,bug发现的三种状态. 1:不能触发Fault 2:触发Fault,但是不能触发Error 3:触发Error,但是不能产生Failure 我们完成的代 ...
- scrollLeft滚动(用animate替代)
原: let checkedLeft1 = $('#dateBox').find('.checked').position().left let checkedLeft2 = $('#dateBox' ...
- 求含有n个因子的最小正整数(n<=1000000)
题目链接:https://ac.nowcoder.com/acm/contest/331/G 思路: 根据唯一分解定理,如果一个数n可以表示成 n=p1a1*p2a2*...*pkak (pi是第i个 ...