##########################文件开始fileDect.py#############################
#encoding:utf8
# Detect file encoding
# Simple method that just chacks that first 1000 lines are valid in each encoding
# and chooses first from set that is valid for all lines checked.
# A better version would allow for a small proportion of failures and rank encodings
# depending on how well they match the input.
import sys 
import os

encodings = [ 
    ['utf-8', 65001, 0], 
    ['cp932', 932, 128],
    ['cp936', 936, 134],
    ['cp949', 949, 129],
    ['cp950', 950, 136],
]

codings = [e[0] for e in encodings]

def EncodingWorks(encoding, text):
    try:
        text.decode(encoding)
        return True
    except UnicodeDecodeError:
        return False
    
# Read up to first 1000 lines of file
if len(sys.argv) > 1 and os.path.isfile(sys.argv[1]):
    with open(sys.argv[1], "rb") as f:
        lineNumber = 1 
        for line in f.readlines():
            # Filter out any encodings that fail
            codings = [c for c in codings if EncodingWorks(c, line)]
            lineNumber += 1
            if lineNumber > 1000:
                break

codingsKnow = False

comment = ''
for c in codings:
    for e in encodings:
        if e[0] == c:
            codingsKnow = True
            codePage, characterSet = e[1:]
            if codePage:
                print('%scode.page=%s' % (comment, codePage))
            if characterSet:
                print('%scharacter.set=%s' % (comment, characterSet))
            # Display other matches as comments so can check results
            comment = '#' 
#如果检测不出文件的编码,将默认编码设置成cp936(GBK)
if codingsKnow==False:
    print 'code.page=936'
    print 'character.set=134'
# Change the caret colour so we can see that something happened
print('caret.fore=#4499FF')
############################文件结束#######################################
然后在配置文件SciTEGlobal.properties中加入
command.discover.properties=python /path/to/fileDetect.py "$(FilePath)" 
即可自动检测文件编码,上面的文件可以检测utf-8,gbk,big5等编码,足够使用。

ps.上面的代码是别人写的。。。在linux上测试通过,需要安装python环境
由于是直接复制成网页的,直接拷贝到代码文件可能有问题

Encodings

SciTE will automatically detect the encoding scheme used for Unicode files that start with a Byte Order Mark (BOM). The UTF-8 and UTF-16 encodings are recognised including both Little Endian and Big Endian variants of UTF-16.

UTF-8 files will also be recognised when they contain a coding cookie on one of the first two lines. A coding cookie looks similar to "coding: utf-8" ("coding" followed by ':' or '=', optional whitespace, optional quote, "utf-8") and is normally contained in a comment:

# -*- coding: utf-8 -*-

For XML there is a declaration:

<?xml version='1.0' encoding='utf-8'?>

For other encodings set the code.page and character.set properties.

scite自动检测文件编码相关推荐

  1. php怎么查自己的文件编码,php检测文件编码的方法示例

    关于文件编码的检测,很多人建议 mb_detect_encoding 检测,可是不知为何不成功,什么都没输出.看到有人写了个增强版,用 BOM 判断的,我果断就无视了,这东西完全不靠谱.最终根据PHP ...

  2. php 检测编码函数,自己写了一个php检测文件编码的函数

    关于文件编码的检测,百度一下一大把都是,但是确实没有能用的. 很多人建议 mb_detect_encoding 检测,可是不知为何我这不成功,什么都没输出. 看到有人写了个增强版,用 BOM 判断的, ...

  3. Python编程笔记(第三篇)【补充】三元运算、文件处理、检测文件编码、递归、斐波那契数列、名称空间、作用域、生成器...

    一.三元运算 三元运算又称三目运算,是对简单的条件语句的简写,如: 简单条件处理: if 条件成立:val = 1else:val = 2 改成三元运算 val = 1 if 条件成立 else 2 ...

  4. python 检测文件编码_[常用] 在Python中检测网页编码

    [常用] 在Python中检测网页编码 在使用Python抓取网页并进行分析时出现这个错误: UnicodeDecodeError: 'utf8' codec can't decode byte 0x ...

  5. java自动猜测文件编码

    2019独角兽企业重金招聘Python工程师标准>>> public static String guessCharset(InputStream inputStream,boole ...

  6. 自动检测文本文件编码是否为GB2312(简体中文),并转换为UTF8编码,附一个GB2312全区对应的utf8编码码表

    gb2312及对应的区码.位码.机内码等原理知识,这里不再赘述,请参考这里http://blog.csdn.net/zhongguoren666/article/details/7738159,下面G ...

  7. 自动检测TXT文件编码

    自动检测文本文件编码的代码(只能简单判断是UTF-8或不是UTF-8),因为一般除了UTF-8之外就是GBK,所以就设置默认为GBK. String charsetDetect(String path ...

  8. Python使用chardet包自动检测编码

    chardet:charset detection 一旦自动检测出编码,就可以解码了. 八种文件打开方式 w:一旦打开文件,文件内容就清空了 r:只读方式打开 a:追加方式打开 r+:先读后写 以上四 ...

  9. python批量转换文件编码

    python批量转换文件编码   3年之前   python 今天在 eclipse 中导入了个之前的 swing 项目,结果跑起来后乱码,检查代码发现竟然一部分 java 文件是 utf-8 编码, ...

最新文章

  1. CVPR 2020丨UDVD:用于可变退化的统一动态卷积超分辨率网络
  2. 12.04 scrum report
  3. Set 的合集 并集 差集
  4. asp.net application,session,static 几种变量生命周期及用法 收藏
  5. 【Python】8000字概括精髓,pandas必知必会50例
  6. Zuul:Pre和Post过滤器(下)
  7. zoj2968 Difference Game
  8. sql server 内存_SQL Server内存性能指标–第3部分– SQL Server Buffer Manager指标和内存计数器
  9. OSChina 周日乱弹 —— 感到孤单了怎么办?
  10. 霍金警告人工智能危险性 称其若失控很难被阻止
  11. Unity读取CSV表格时出现中文乱码处理方式
  12. ABAP 学习笔记一
  13. NDT方法总结与公式推导
  14. Mac触控板设置以及使用
  15. 前端实现组织结构列表
  16. 中小网站如何使用谷歌Adsence国际化优势赚钱?
  17. python oracle 工具类,python连接Oracle工具类
  18. Django基础之MVT
  19. 如何在Outlook中将所有邮件显示为纯文本
  20. 使用Easyexcel动态生成excel

热门文章

  1. 原生JS实现Promise(详解)
  2. 【聆思CSK6 视觉AI开发套件试用】体验AI功能
  3. JQuery Marquee插件(无缝滚动效果)- marquee.js
  4. 聚类分析的评价指标(性能度量)
  5. 猫眼上市后的第一份财报:不安与野心同在
  6. linux系统压缩与解压缩工具使用方法
  7. vue3.0实现移动端自适应
  8. a2dp sink 在android kk和L实现的区别
  9. 新大西洋海底光缆:传输速度达160Tbps
  10. 安卓电子书格式_6 款值得收藏的电子书阅读软件推荐