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)
随机推荐
- ios 真机测试与发布详细流程,基于最新的开发者网站,ios7,xcode5(有截图的哦)[[[第一部分真机测试]]]
转载于:http://blog.csdn.net/lv_ruanruan/article/details/14446597 真机测试及发布详细流程,最新版 第一次一个人搞一个项目,我们老大规定,一个周 ...
- RNN LSTM 介绍
[RNN以及LSTM的介绍和公式梳理]http://blog.csdn.net/Dark_Scope/article/details/47056361 [知乎 对比 rnn lstm 简单代码] ...
- ubuntu搭建ftp服务器
(1).首先用命令检查是否安装了vsftpd vsftpd -version 如果未安装用一下命令安装 sudo apt-get install vsftpd 安装完成后,再次输入vsftpd -v ...
- CentOS 下安装 OpenOffice4.0
一.更新服务器 yum源 [root@APP2 /]# yum clean all [root@APP2 /]# yum makecache [root@APP2 /]# yum update 1.首 ...
- C语言实现大数四则运算
一.简介 众所周知,C语言中INT类型是有限制,不能进行超过其范围的运算,而如果采用float类型进行运算,由于float在内存中特殊的存储形式,又失去了计算的进度.要解决整个问题,一种解决方法是通过 ...
- android笔记:BroadcastReceiver
android允许应用程序自由地发送和接收广播. 广播是通过Intent进行数据传递的.接收广播则通过Broadcast Receiver(广播接收器)实现. 广播分为:标准广播和有序广播. 标准广播 ...
- cdoj203-Islands 【并查集】
http://acm.uestc.edu.cn/#/problem/show/203 Islands Time Limit: 30000/10000MS (Java/Others) Memor ...
- 安装使用phpStudy在本机配置php运行环境
前言: php开发的初学者,强烈推荐使用phpStudy集成环境,一方面这个的确很好用(本人电脑安装了jspStudy,可以同时调试php和jsp),另一方面呢,虽然本人是技术控,但对这些繁杂的安装部 ...
- Python并发讨论
手段有多线程,多进程,协程. 对于多线程: 由于GIL(全局解释器锁)的存在,多线程实际是单线程的,不能发挥多核的作用: 但对于IO密集型程序,多线程对于效率是有提高的,由于阻塞时,可能会切换到别的线 ...
- ios 处理WKContentView的crash
http://www.jianshu.com/p/7ef5814a871b 解决WKContentView没有isSecureTextEntry方法造成的crash 程序中有web页面,使用W ...