使用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. 排排看(p20)

    public class paixu{public static void main(String[] args){int x=3;if(x>2){System.out.print(" ...

  2. P2P通信标准协议(二)之TURN

    上一篇P2P通信标准协议(一)介绍了在NAT上进行端口绑定的通用规则,应用程序可以根据这个协议来设计网络以外的通信. 但是,STUN/RFC5389协议里能处理的也只有市面上大多数的Cone NAT( ...

  3. 如何提高码农产量,基于ASP.NET MVC的敏捷开发框架之工作流开发随笔三

    前言 “厂长,APP的那几个功能都差不多了,接下来要做工作流,工作流这东西我完全没概念啊.” “查尔斯,一般来说工作流就是指将指定的数据.文件.任务按照预定的规则进行传递流转.比如说你要请假,拿个请假 ...

  4. OneThink框架的文章详情页分页

    Application/Home/Controller/ArticleController.class.php的detail函数修改结果如下: /* 文档模型详情页 */public function ...

  5. 原型设计工具——Axure

    1,百度百科 http://baike.baidu.com/view/3332366.htm?from_id=5056136&type=syn&fromtitle=axure& ...

  6. [转载]Delphi Tokyo 10.2.3发布了

    转载:http://blog.sina.com.cn/s/blog_44fa172f0102wwwg.html 今早最好的消息,Delphi 10.2.3如期发布,下载地址:http://altd.e ...

  7. centos和ubuntu下pycharm无法输入中文的解决办法

    编辑启动的脚本文件 vim /usr/bin/pycharm ubuntu下添加 export GTK_IM_MODULE=fcitx export QT_IM_MODULE=fcitx export ...

  8. java源码阅读LinkedBlockingQueue

    1类签名与简介 public class LinkedBlockingQueue<E> extends AbstractQueue<E> implements Blocking ...

  9. map-reduce任务的执行流程

    (1).客户端提交一个mr的jar包给JobClient(提交方式:hadoop jar ...) (2).JobClient通过RPC和JobTracker进行通信,返回一个存放jar包的地址(HD ...

  10. 转:使用gradle 构建编译程序

    https://rinvay.github.io/android/2015/04/09/Build-Android-with-Gradle/