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. requireJS-初识

    浅谈requireJS 2016-04-26 21:44 by 猴子猿, 726 阅读, 0 评论, 收藏, 编辑 项目中大都使用模块化开发,requireJS作为AMD模块开发的典范,所以有必要学习 ...

  2. Matlab学以致用 - 曲线拟合

    曲线拟合 使用Matlab自带的polyfit函数,可以很方便地根据现有样本数据进行多项式曲线拟合,为了有直观感受,先上例程,如下所示: x = -:; % 样本数据x坐标 y = *x.^ + *x ...

  3. dll is in timestamps but is not known in guidmapper… 错误

    解决方法是:在Assets下右键,Reimport All

  4. request.getParameterMap()获得Map中的数据

    今天使用request.getParameterMap()获得Map中的数据时,使用        Map map=request.getParameterMap();               i ...

  5. 启动startUML的时候报错“"Exception EOleSysError in module StarUML.exe at 000AD559. "

    win7操作系统在安装startuml的时候总是报错 出现了如下错误提示:"Exception EOleSysError in module StarUML.exe at 000AD559. ...

  6. hibernate hql limit的实现方式

    query.setFirstResult(startIndex);//开始索引query.setMaxResults(maxSize);//取几条 如 limit 1,5HQL:query.setFi ...

  7. Delphi 操作键盘按下和释放操作

    Unit Unit1; Interface Uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ...

  8. stl-stack+括号配对问题

    栈:stl的一种容器,遵循先进后出原则,,只能在栈的顶部操作,就像放盘子一样,洗好的盘子叠在上面,需要用时也是先从顶部拿.不允许被遍历,没有迭代器 基本操作: 1.头文件#include<sta ...

  9. post与get的区别

    GET请求在URL中传送的参数大多数浏览器限制该长度为2kb的,而POST没有. GET比POST更不安全,因为参数直接暴露在URL上,所以不能用来传递敏感信息. GET参数通过URL传递,POST放 ...

  10. linux 安装SAMtools,bcftools,htslib,sratoolkit,bedtools,GATK,TrimGalore,qualimap,vcftools,bwa

    --------------------安装Samtools---------------------------------------------------------------------- ...