参考:http://my.oschina.net/waterbear/blog/149852

chardet模块,能够实现文本编码的检查,

核心代码:

import chardet
chardet.detect(content)['encoding']

实现目录java文件转码:

#-*- coding: utf-8 -*-

import codecs
import os
import shutil
import re
import chardet def convert_encoding(filename, target_encoding):
# Backup the origin file.
shutil.copyfile(filename, filename + '.bak') # convert file from the source encoding to target encoding
content = codecs.open(filename, 'r').read()
source_encoding = chardet.detect(content)['encoding']
print source_encoding, filename
content = content.decode(source_encoding) #.encode(source_encoding)
codecs.open(filename, 'w', encoding=target_encoding).write(content) def main():
for root, dirs, files in os.walk(os.getcwd()):
for f in files:
if f.lower().endswith('.java'):
filename = os.path.join(root, f)
try:
convert_encoding(filename, 'utf-8')
except Exception, e:
print filename def process_bak_files(action='restore'):
for root, dirs, files in os.walk(os.getcwd()):
for f in files:
if f.lower().endswith('.java.bak'):
source = os.path.join(root, f)
target = os.path.join(root, re.sub('\.java\.bak$', '.java', f, flags=re.IGNORECASE))
try:
if action == 'restore':
shutil.move(source, target)
elif action == 'clear':
os.remove(source)
except Exception, e:
print source if __name__ == '__main__':
# process_bak_files(action='clear')
main()

另,参考:Python 的中文编码处理

http://in355hz.iteye.com/blog/1860787

  1. # 检查标准输出流的编码
  2. print sys.stdout.encoding
  1. # 无论如何,请用 linux 系统的当前字符集输出:
  2. if sys.stdout.encoding is None:
  3. enc = os.environ['LANG'].split('.')[1]
  4. sys.stdout = codecs.getwriter(enc)(sys.stdout)  # 替换 sys.stdout
  1. # 使得 sys.getdefaultencoding() 的值为 'utf-8'
  2. reload(sys)                      # reload 才能调用 setdefaultencoding 方法
  3. sys.setdefaultencoding('utf-8')  # 设置 'utf-8'

python 检测文件编码等的更多相关文章

  1. Python编程笔记(第三篇)【补充】三元运算、文件处理、检测文件编码、递归、斐波那契数列、名称空间、作用域、生成器

    一.三元运算 三元运算又称三目运算,是对简单的条件语句的简写,如: 简单条件处理: if 条件成立: val = 1 else: val = 2 改成三元运算 val = 1 if 条件成立 else ...

  2. python检测文件的MD5值

    python检测文件的MD5值MD5(单向散列算法)的全称是Message-Digest Algorithm 5(信息-摘要算法),经MD2.MD3和MD4发展而来.MD5算法的使用不需要支付任何版权 ...

  3. php -- php检测文件编码的方法示例

    <?php /** * 检测文件编码 * @param string $file 文件路径 * @return string|null 返回 编码名 或 null */ function det ...

  4. Python读取文件编码及内容

    Python读取文件编码及内容 最近做一个项目,需要读取文件内容,但是文件的编码方式有可能都不一样.有的使用GBK,有的使用UTF8.所以在不正确读取的时候会出现如下错误: UnicodeDecode ...

  5. python检测文件的MD值

    使用hashlib模块,可对文件MD5一致性加密验证: #python 检测文件MD5值 #python version 2.6 import hashlib import os,sys #简单的测试 ...

  6. python 的文件编码处理

    python的文件编码处理有点粗鲁 1.不管文件原来是编码类型,读入后都转换成Unicode的编码 2.写入文件时,write函数把变量以读入文件的编码方式写入(根据open(path,mode,en ...

  7. Python中文件编码的检测

    前言: 文件打开的原则是“ 以什么编码格式保存的,就以什么编码格式打开 ”,我们常见的文件一般是以“ utf-8 ”或“ GBK ”编码进行保存的,由于编辑器一般设置了默认的保存和打开方式,所以我们在 ...

  8. python 修改文件编码方式

    import chardet import os def strJudgeCode(str): return chardet.detect(str) def readFile(path): try: ...

  9. python 转化文件编码 utf8

    使用visual studio最大的一个问题就是文件编码问题,当文件中有中文时,visual studio 会默认为区域编码,也就是gb2312,如果想跨平台或者不用vs编译的话,就会因为编码问题导致 ...

随机推荐

  1. 崩溃信息:Message from debugger: Terminated due to signal 9

    是因为你在调试的时候主动了结束了程度,如上滑结束了程序

  2. 关于windows10调试应用注册失败

    搜索了一些方法,都是win8系统的应用程序解决方案,修改应用程序的包名.也尝试修改了一些,但是失败,任然报错,以前在这个机器上面是能正常调试的,唯一的不同点是就是系统升级到10166了,于是去设置里面 ...

  3. SQL Constraint/Index

    1.SQL Constraint Integrity Constraints are used to apply business rules for the database tables. The ...

  4. attempt to write a readonly database 的解决办法

    这个问题导致我的unity项目崩溃,以至于无法打开. 第一次出现这个问题是因为在Lighting窗口中build按钮下点击了clear all baked datas,导致unity强制退出,并给出上 ...

  5. 几个app maker的网站

    简网APP工场:http://www.cutt.com/app 爱传iappk:http://www.iappk.com 安米网:http://www.appbyme.com/mobcentACA/i ...

  6. Trie,HDU1875world puzzle

    附上代码 #include<iostream> #include<cstdio> #include<string> #include<cstring> ...

  7. mouseover与mouseenter与mousemove的区别mouseout与mouseleave的区别

    <html> <head> <title></title> </head> <body> <p> 当鼠标进入div1 ...

  8. 把DataSet转换成JSON

    /// <summary> /// dataTable转换成Json格式 /// </summary> /// <param name="dt"> ...

  9. [CSS]white-space 属性详解

    实例 规定段落中的文本不进行换行: p { white-space: nowrap } 可能的值 值 描述 normal 默认.空白会被浏览器忽略. pre 空白会被浏览器保留.其行为方式类似 HTM ...

  10. (转载)Delphi TStringList的用法

    Delphi TStringList的用法 TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. TStringList 常用方法与属性: var List: TStringL ...