参考: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. 九度OJ 1528 最长回文子串 -- Manacher算法

    题目地址:http://ac.jobdu.com/problem.php?pid=1528 题目描述: 回文串就是一个正读和反读都一样的字符串,比如"level"或者"n ...

  2. msyql判断记录是否存在的三种方法

    1. select count(*) from .... 这种方法最常见但是效率比较低,因为它需要扫描所有满足条件的记录 2. select 1 from xxxtable where .... 这种 ...

  3. QtSQL学习笔记(2)- 连接到数据库

    要使用QSqlQuery或者QSqlQueryModel访问一个数据库,首先需要创建并打开一个或多个数据库连接(database connections). 一般地,数据库连接是根据连接名(conne ...

  4. Linux---文件类型及权限操作

    文件类型: 用ls命令查看目录下所属文件时,每行的第一个字母标识着文件对应的文件类型 '-':代表普通文件 'd':代表目录 'c':字符设备文件 'b':块设备文件 's':套接字文件 'l':符号 ...

  5. apache开启gzip的方法

    在Apache中开启gzip压缩方法为: 1. 在httpd.conf 或者博客根目录的.htaccess文件中加入如下规则(Apache服务器需要支持 mod_deflate) 本文出处参考:htt ...

  6. 机器学习实战——k-近邻算法

    本章内容 ================================ (一)什么是k-近邻分类算法 (二)怎样从文件中解析和导入数据 (三)使用Matplotlib创建扩散图 (四)对数据进行归 ...

  7. PL/SQL — 函数

    函数通常用于返回特定的数据.其实质是一个有名字的PL/SQL块,作为一个schema对象存储于数据库,可以被反复执行.函数通常被作为一个表达式来调用或存储过程的一个参数,具有返回值.   一.建立函数 ...

  8. 8.MVC框架开发(URL路由配置和URL路由传参空值处理)

    1.ASP.NET和MVC的路由请求处理 1)ASP.NET的处理 请求---------响应请求(HttpModule)--------处理请求(HttpHandler)--------把请求的资源 ...

  9. MVC-内容详情页显示内容

    @model InfoDataProvider.DataModel.FAQ_ContentUser 内容Content字段:如果里面有html标签. @Html.DisplayFor(p => ...

  10. windows azure programing

    http://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-get-started-vs2012/ http:// ...