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)

随机推荐

  1. 关于frameset与iframe的使用

    <frameset>与<body>标签同级,是不能同时存在的,<frameset>是把当前页面进行分割. frameset.html: <!DOCTYPE h ...

  2. HTML转义

    HTML转义 模板对上下文传递的字符串进行输出时,会对以下字符自动转义 小于号< 转换为< 大于号> 转换为> 单引号' 转换为' 双引号" 转换为 " 与 ...

  3. CentOS 7安装配置Redis数据库

    Redis源码获取 1.进入Redis官网获取Redis最新稳定版下载地址 2.通过wget命令下载 Redis 源代码.   Redis编译 1.通过tar -xvf redis-3.0.2.tar ...

  4. nginx的Mainline version、Stable version、Legacy version

    Nginx官网提供了三个类型的版本Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版Stable version:最新稳定版,生产环境上建议使用的版 ...

  5. EasyUI 导出页面到Excel中

    <script type="text/javascript"> <!-- js --> /*================================ ...

  6. 域名相关:DNS A记录 NS记录 MX记录 CNAME记录

    1. DNSDNS:Domain Name System 域名管理系统 域名是由圆点分开一串单词或缩写组成的,每一个域名都对应一个惟一的IP地址,这一命名的方法或这样管理域名的系统叫做域名管理系统.D ...

  7. 使用Fiddler发送POST请求

    使用Fiddler发送POST请求 在测试过程中,有时会遇到需要修改请求中带的参数,去验证权限的安全问题,但是一些post请求,我们在浏览器中不能直接修改他的参数,然后去提交验证. 而fiddler可 ...

  8. nat 类型及打洞原理

    nat 类型分4种 1.全锥形 full cone A 与 主机B交互,nat转换 A的内部地址及端口为  ip1 port1,ip1和port1为对外地址,任何机器能访问. 2.ip 受限制(对B而 ...

  9. 136. Single Number (Bit)

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  10. 57. Insert Interval (Array; Sort)

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...