python 转化文件编码 utf8
使用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的更多相关文章
- Python读取文件编码及内容
Python读取文件编码及内容 最近做一个项目,需要读取文件内容,但是文件的编码方式有可能都不一样.有的使用GBK,有的使用UTF8.所以在不正确读取的时候会出现如下错误: UnicodeDecode ...
- python 的文件编码处理
python的文件编码处理有点粗鲁 1.不管文件原来是编码类型,读入后都转换成Unicode的编码 2.写入文件时,write函数把变量以读入文件的编码方式写入(根据open(path,mode,en ...
- python声明文件编码,必须在文件的第一行或第二行
#coding=utf-8和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型 注意的两点: 1.声明必须在文件的第一行或第二行: 2.coding后面必须紧跟冒号或等号,#c ...
- Python中文件编码的检测
前言: 文件打开的原则是“ 以什么编码格式保存的,就以什么编码格式打开 ”,我们常见的文件一般是以“ utf-8 ”或“ GBK ”编码进行保存的,由于编辑器一般设置了默认的保存和打开方式,所以我们在 ...
- python 修改文件编码方式
import chardet import os def strJudgeCode(str): return chardet.detect(str) def readFile(path): try: ...
- python 写文件,utf-8问题
写文件报数据. 同样的编码. 含中文字段的输出文件 编码为utf-8 无中文的却是asc import codecstxt = u”qwer”file=codecs.open(“test”,”w”,” ...
- python 检测文件编码等
参考:http://my.oschina.net/waterbear/blog/149852 chardet模块,能够实现文本编码的检查, 核心代码: import chardet chardet.d ...
- python的文件编码注释
在python源文件的第一行或第二行写入如下内容: # -*- coding:gbk -*- # 设置源文件编码格式为gbk 或 # -*- coding:utf-8 -*- # 设置源文件编码格式为 ...
- python写入文件编码报错
decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码. encode的作用是将u ...
随机推荐
- 【最短路】【最大流】bzoj3931 [CQOI2015]网络吞吐量
跑出最短路图,然后把结点拆点跑最大流. #include<cstdio> #include<queue> #include<cstring> #include< ...
- 【数论】【枚举约数】【欧拉函数】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< ...
- 收藏起来,史上最全的 MySQL 高性能优化实战总结!
转自:https://mp.weixin.qq.com/s/sRsJzFO9dPtKhovJNWN3Dg 一.前言 MySQL 对于很多 Linux 从业者而言,是一个非常棘手的问题,多数情况都是因为 ...
- Ubuntu 16.04下使用UNetbootin制作的ISO镜像为U盘启动出现:Missing Operating System (mbr.bin)
通过以下方式进行排查: 1.确定U盘是否真的有启动系统 2.分区是否已经标记为激活状态,尤其使用了Fdisk进行分区时,如果分区>=2时默认是不设置激活分区. 比如下面是通过Fdisk进行设置分 ...
- Android API level 版本对应关系
详情地址:http://developer.android.com/guide/topics/manifest/uses-sdk-element.html Platform Version API L ...
- react使用echarts
1.安装echarts: npm install echarts --save 2.制作线性图组件,只引入echart必要的js内容 /** * Created by yongyuehuang on ...
- virtualenv、virtualenvwrapper安装和使用;Mac os的特殊性
[sudo] pip install virtualenv 或者[sudo] pip3 install virtualenv [sudo]可用可不用 pip/pip3 install virtuale ...
- windows 7系统搭建PHP网站环境
2.新建数据库打开浏览器,输入http://localhost:9999或者http://127.0.0.1:9999回车填写用户名root和密码回车登录点击权限-添加新用户填写用户名,主机选择本地, ...
- Snapdragon profiler
这个debugger似乎看不了constant buffer 看不了memory but有个很神奇的功能 改shader直接在手机上显示结果 注意 需要unity build的时候勾 Script D ...
- builder pattern
design patterns 结合书本和这个网站的这个系列的文章来看: https://www.tutorialspoint.com/design_pattern/builder_pattern.h ...