pycharm - 怎么判断python代码文件自身的编码类型?
PHP中文网
PHP中文网 2017-04-18 09:37:35
0
1
389

比如,新建python代码文件file.py,
再file.py首行写:#coding=utf
这是指定file.py文件使用utf-8编码.
我的问题是:在file.py代码中,怎么使用代码获取到file.py文件自身的编码类型?
通常用两个方法也可以获取到,但我想问的是除此之外,还有更直接可靠的方法吗?
通常方法一:读取file.py文件的内容获取第一行内容,然后正则或者字符串判断.
通常方法二:读取file.py文件的内容,然后直接使用chardet模块来判断该内容的编码.
除了以上两个思路之外,python是否有内置方法或者模块来方便的获取到py代码文件自身的编码类型呢?
在pycharm中,这个ide是可以及时感知到首行设置的代码的(比如首行原来是#coding=u8,修改为#coding=gbk之后,pycharm自动尝试将当前编辑的py代码文件由utf-8编码转为gbk编码),不知道pycharm是怎么做的?
多谢!

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
洪涛

No answer to the question, just two sentences written in ink:

  1. To be honest, the encoding of the file has nothing to do with coding=utf-8. The encoding of the file is not bound by this comment

  2. What the computer saves is only 01, it just depends on which format you use to decode it. chardet You should try your best and guess which one has the least errors

  3. I have never used pycharm, but I guess pycharm is responsible for the system encoding format or it is set by you.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!