Python coding problem

CampThere was a lot of python2 coding.

Understanding the encoding of pyhon2

python2There are only two kinds of string types:Type STR: B'xxx'That is, the str type is the encoded type, and Len () is calculated bybyte.Unicode type: len (), calculated by Unicode character.
python2Open file read string is STR type, without encoding parameter.Recommended reading and writing files under python2codecs packageCodecs.open, codecs.write can specify encoding.

 

python3Coding

python3There are two kinds of string types:Type STR: u'xxx'The str type is the uncoded Unicode. Pay attention to the distinction between python2 andType bytes: type after encoding

python3Open files with encoding parameters that can be read in a specified encoding mode and read as STR type (unicode) strings

 

Python 2 Treat strings as a native bytes type instead of Unicode.
Python 3 All strings are Unicode types.

utf-8The code is compatible with ASCII encoding, and the characters encoded by asscii are the same as those encoded by UTF-8.

Leave a Reply

Your email address will not be published. Required fields are marked *