Python 2.7 Character Encoding

If you write python programs and do somethings about characters , maybe there are some matters about character gibberish often happens. The matter has bothered me for a long time, so I spent some time to thoroughly understand this problem.

There are two blogs is useful:

About character encoding in python 2.7, I think it is important to note the following points:

  1. We can think that all the Chinese words are somethings be encrypted , but computer stored it as true words , we can't read this word directly,the character encoding rules like utf-8, gbk, unicode are the password.

  2. If you open a file and read it, you should tell python what is the file's encoding, if there are not any word about this, python will use the environment encoding, like the script first line:

#coding = utf-8

you should decode you file characters by decode() method, python can use this password to decode file and show the Chinese words or others words.
After read, if you want to save this file, you should encode Chinese words which you can read directly by encode() method, python can use this password to encode the Chinese words to some characters, and save it in disk.

  1. Python use unicode as defult password to decode words, if you give if a str, and not tell the encoding type, it can't tell you the true word what you want.

  2. You can't use decode() method in unicode, and can't use encode() method in str.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 终于终于到周末啦 终于终于可以好好休息一下 放松一下 该努力时好好努力,该放松时好好放松 这也是我一直努力和坚持的...
    快乐拉拉阅读 1,033评论 0 1
  • 2017年4月9日打卡 周日,雨过天晴,潮湿阴冷。 子冉无意中发现一个介绍家乡的图片,截图下来做个资料。其实图片上...
    沈曼柔阅读 1,534评论 9 3
  • 这里是引用,本文主要是《第一行代码》阅读笔记!由于刚开始学习Android,所以肯定有很多见解不成熟。希望看到的人...
    咸鱼而已阅读 7,414评论 0 5
  • 生活中其实没什么大事,但每一件小事聚合起来,就铸造了一个人的样子。想做成一件事,最怕的不是没运气、没钱、没伯乐,而...
    MichaelChengy阅读 4,979评论 0 3