博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
将代码源文件由utf8转为GBK
阅读量:4111 次
发布时间:2019-05-25

本文共 1195 字,大约阅读时间需要 3 分钟。

souceinsight打开utf8的文件时候,汉字显示乱码。将.c和.h的源文件转为gbk格式的方便显示。

比之前用chardet.detect写的更好点,因为发现有的源文件里既有utf8的中文,又有gbk的中文。

#coding:utf-8# 每个文件,对于每行内容,先转utf8,如果utf8有误,则是gbk#import osimport sysreload(sys)sys.setdefaultencoding('utf-8')def ReadFile(filename,decoding="utf-8",encoding="gbk"):    f = open(filename,"r")     content = f.readlines()    f.close()    for i in range(len(content)):        try:            content[i] = content[i].decode(decoding).encode(encoding)            #print content[i];        except:            content[i] = content[i].decode(encoding,errors="ignore").encode(encoding,errors="ignore")    return contentdef WriteFile(filename,content):    f = open(filename,"w")    f.writelines(content)    f.close()        def utf8_to_ansi(filename):    content = ReadFile(filename,decoding="utf-8",encoding="gbk")    WriteFile(filename,content)    print "change file %s"%filename    def FilesFormat(path):    for root, dirs, files in os.walk(path):        for f in files:            if f[-2:] == '.c' or f[-2:] == '.h':   #只转.c和.h                utf8_to_ansi(root+"\\"+f)                               if __name__ == "__main__":    path=u" "  #目录    FilesFormat(path)    print "ALL FILES DONE!"

转载地址:http://ekesi.baihongyu.com/

你可能感兴趣的文章
Think more, do more!
查看>>
Linux子系统系列-时钟子系统
查看>>
六一悄悄的过了
查看>>
Linux子系统系列-TTY
查看>>
Linux子系统系列-PCI
查看>>
Linux子系统系列-网络
查看>>
关于Zbar和ZXing这两个无比强大的二维码和条形码识别工具
查看>>
ASIHTTPRequest类库简介和使用说明
查看>>
ios中http 和https 协议的访问
查看>>
PHP+Apache+MySQL经典搭配,创建环境一 PHP安装(转载并修改)
查看>>
PHP+Apache+MySQL经典搭配,创建环境二 Apache Web服务器安装(转载并修改)
查看>>
搭建PHP后台开发环境, XAMPP
查看>>
su和sudo的区别与使用, 如果有时提示说权限不够, 而使用sudo后也同样提示,可以试试su
查看>>
使用fedora进行Android源码的编译
查看>>
SQLite3-增删改查 转载并修改
查看>>
MPMoviePlayerViewController播放媒体文件时在ios5.0上的区别--修改
查看>>
iAd和admob混用
查看>>
UIScrollView无法响应touch事件的解决办法
查看>>
技术普及帖:你刚才在淘宝上买了一件东西
查看>>
普及贴:关于码率bitrate,帧率frame rate,分辨率的区别
查看>>