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

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

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

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

  1. import codecs
  2. import os
  3. import sys
  4. import shutil
  5. import re
  6. import chardet
  7.  
  8. convertdir = sys.argv[1]
  9. convertfiletypes = [
  10. ".cpp",
  11. ".h",
  12. ".hpp"
  13. ]
  14.  
  15. def convert_encoding(filename, target_encoding):
  16. # Backup the origin file.
  17.  
  18. # convert file from the source encoding to target encoding
  19. content = codecs.open(filename, 'r').read()
  20. source_encoding = chardet.detect(content)['encoding']
  21. if source_encoding != 'utf-8':
  22. print source_encoding, filename
  23. content = content.decode(source_encoding, 'ignore') #.encode(source_encoding)
  24. codecs.open(filename, 'w', encoding=target_encoding).write(content)
  25.  
  26. def main():
  27. for root, dirs, files in os.walk(convertdir):
  28. for f in files:
  29. for filetype in convertfiletypes:
  30. if f.lower().endswith(filetype):
  31. filename = os.path.join(root, f)
  32. try:
  33. convert_encoding(filename, 'utf-8')
  34. except Exception, e:
  35. print filename
  36.  
  37. if __name__ == '__main__':
  38. 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. 【最短路】【最大流】bzoj3931 [CQOI2015]网络吞吐量

    跑出最短路图,然后把结点拆点跑最大流. #include<cstdio> #include<queue> #include<cstring> #include< ...

  2. 【数论】【枚举约数】【欧拉函数】bzoj2705 [SDOI2012]Longge的问题

    ∵∑gcd(i, N)(1<=i <=N) =k1*s(f1)+k2*s(k2)+...+km*s(km) {ki是N的约数,s(ki)是满足gcd(x,N)=ki(1<=x< ...

  3. 收藏起来,史上最全的 MySQL 高性能优化实战总结!

    转自:https://mp.weixin.qq.com/s/sRsJzFO9dPtKhovJNWN3Dg 一.前言 MySQL 对于很多 Linux 从业者而言,是一个非常棘手的问题,多数情况都是因为 ...

  4. Ubuntu 16.04下使用UNetbootin制作的ISO镜像为U盘启动出现:Missing Operating System (mbr.bin)

    通过以下方式进行排查: 1.确定U盘是否真的有启动系统 2.分区是否已经标记为激活状态,尤其使用了Fdisk进行分区时,如果分区>=2时默认是不设置激活分区. 比如下面是通过Fdisk进行设置分 ...

  5. Android API level 版本对应关系

    详情地址:http://developer.android.com/guide/topics/manifest/uses-sdk-element.html Platform Version API L ...

  6. react使用echarts

    1.安装echarts: npm install echarts --save 2.制作线性图组件,只引入echart必要的js内容 /** * Created by yongyuehuang on ...

  7. virtualenv、virtualenvwrapper安装和使用;Mac os的特殊性

    [sudo] pip install virtualenv 或者[sudo] pip3 install virtualenv [sudo]可用可不用 pip/pip3 install virtuale ...

  8. windows 7系统搭建PHP网站环境

    2.新建数据库打开浏览器,输入http://localhost:9999或者http://127.0.0.1:9999回车填写用户名root和密码回车登录点击权限-添加新用户填写用户名,主机选择本地, ...

  9. Snapdragon profiler

    这个debugger似乎看不了constant buffer 看不了memory but有个很神奇的功能 改shader直接在手机上显示结果 注意 需要unity build的时候勾 Script D ...

  10. builder pattern

    design patterns 结合书本和这个网站的这个系列的文章来看: https://www.tutorialspoint.com/design_pattern/builder_pattern.h ...