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

所以写了个python脚本来检测原文件编码并转换为目标编码,以下代码以目标编码为utf-8为例:

需要安装chardet,详情:https://pypi.python.org/pypi/chardet

使用方法:python to_utf8.py /my_project/src

import codecs
import os
import sys
import shutil
import re
import chardet convertdir = sys.argv[1]
convertfiletypes = [
".cpp",
".h",
".hpp"
] def convert_encoding(filename, target_encoding):
# Backup the origin file. # convert file from the source encoding to target encoding
content = codecs.open(filename, 'r').read()
source_encoding = chardet.detect(content)['encoding']
if source_encoding != 'utf-8':
print source_encoding, filename
content = content.decode(source_encoding, 'ignore') #.encode(source_encoding)
codecs.open(filename, 'w', encoding=target_encoding).write(content) def main():
for root, dirs, files in os.walk(convertdir):
for f in files:
for filetype in convertfiletypes:
if f.lower().endswith(filetype):
filename = os.path.join(root, f)
try:
convert_encoding(filename, 'utf-8')
except Exception, e:
print filename if __name__ == '__main__':
main()

python 转化文件编码 utf8的更多相关文章

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

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

  2. python 的文件编码处理

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

  3. python声明文件编码,必须在文件的第一行或第二行

    #coding=utf-8和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型 注意的两点: 1.声明必须在文件的第一行或第二行: 2.coding后面必须紧跟冒号或等号,#c ...

  4. Python中文件编码的检测

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

  5. python 修改文件编码方式

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

  6. python 写文件,utf-8问题

    写文件报数据. 同样的编码. 含中文字段的输出文件 编码为utf-8 无中文的却是asc import codecstxt = u”qwer”file=codecs.open(“test”,”w”,” ...

  7. python 检测文件编码等

    参考:http://my.oschina.net/waterbear/blog/149852 chardet模块,能够实现文本编码的检查, 核心代码: import chardet chardet.d ...

  8. python的文件编码注释

    在python源文件的第一行或第二行写入如下内容: # -*- coding:gbk -*- # 设置源文件编码格式为gbk 或 # -*- coding:utf-8 -*- # 设置源文件编码格式为 ...

  9. python写入文件编码报错

    decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码. encode的作用是将u ...

随机推荐

  1. 重写规则为什么要options +followsymlinks

    重写规则为什么要options +followsymlinks Web服务器的Apache安装编译成功mod_rewrite编译成模块,但当我在网站根目录下放了一个.htaccess配置文件,却得到了 ...

  2. 如何设断点????-----使用WinDbg调试SQL Server查询

    http://www.cnblogs.com/woodytu/p/4665427.html http://www.sqlservercentral.com/blogs/aschenbrenner/20 ...

  3. netstat 用法

    https://linux.cn/article-2434-1.html Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),ma ...

  4. delphi执行cmd命令和bat文件

    转载地址:http://blog.csdn.net/hutao1101175783/article/details/42807063 cmd:='echo d | Xcopy '+BasePath+' ...

  5. delphi中使用mediaplayer控件播放音乐

    新建一个名字为media的文件夹,把要播放的音乐文件放在这个文件夹里. ExtractFilePath是用来获得产生的可执行程式所在的路径,因为我们把要播放的音乐文件放在了可执行程式的那个根目录下. ...

  6. 【docker】查看docker镜像的版本号TAG,从远程仓库拉取自己想要版本的镜像

    要想查看镜像的版本好TAG,需要在docker hub查看 地址如下:https://hub.docker.com/r/library/ 进入之后,在页面左上角搜索框搜索, 例如搜索redis 搜索完 ...

  7. 【网络】再谈select, iocp, epoll,kqueue及各种I/O复用机制 && Reactor与Proactor的概念

    首先,介绍几种常见的I/O模型及其区别,如下: blocking I/O nonblocking I/O I/O multiplexing (select and poll) signal drive ...

  8. Windows命令行报错:'findstr' 不是内部或外部命令,也不是可运行的程序或批处理文件

    环境变量Path中追加:%SystemRoot%/system32;%SystemRoot%;

  9. junit4单元測试总结

    junit4单元測试总结 本文开发环境为myeclipse10.7 1.  准备工作 1.1. 选择须要单元測试的文件 创建mavenproject.右击须要单元測试的文件,选择New->oth ...

  10. 解决ie下载apk后更改后缀名为.zip的问题

    转自:http://www.cnblogs.com/niuniu/archive/2012/03/06/2381811.html 解决: 服务器添加MIME类型: application/vnd.an ...