The second type of module 3.1 Bytes

1 data storage drives, hard disk can store binary

22 binary - "Decimal - asscii / gbk / utf-8 unicode

Data stored to the hard disk, it is necessary to turn to the corresponding encoded binary storage

Text - "utf-8 / gbk -" 2 hex

Picture - "jpg / png -" 2 hex

Music - "MP3 / wav--" 2 binary

bytes type, expressed in hexadecimal form, two hexadecimal form a byte, to b '' is identified, byte strings

For example:

S = >>> " small circle " 
>>> S
 ' small circle ' 
>>> s.encode ( " UTF-. 8 " )
b'\xe5\xb0\x8f\xe5\x9c\x86\xe5\x9c\x88'

Default encoding py3 file is utf-8, python as you specify the encoding compiled binary 

f = open("byte.txt","w", encoding= 'gbk')

f.write ( " Hello Future! " )
f.write ( " \ the n-2 Hello future! " )
f.close( )

b = binary open the file in binary form

f = open("byte.txt","wb")

# F.write ( "Hello Future!") 
F.write ( " \ Hello future the n-2! " .Encode ( " GBK " ))
f.close( )

bytes type

1 character save hard disk bytes to be compiled

2 network transmission, characters to be compiled bytes 

Guess you like

Origin www.cnblogs.com/kissfire008/p/11735870.html