Python Learning note 015--file Common operations binary (binary file)

Source: Internet
Author: User

1-byte (byte) units

1KB = 2*10 Bytes

1MB = 1KB * 1024x768 = 2**20 Bytes

1GB = 1KB * 1K * 1K = 2**30 Bytes

# 1byte = 8bit (bit)

22 Binary files

The binary file is stored in bytes (byte),

File read and write methods that do not differentiate content by line break (\ n)

3 Open Modes Mode

' B '

4-byte String bytes

A byte string is also called a sequence of bytes

Data that is stored in bytes

Immutable byte sequence at byte-string

(If you don't understand it, you can refer to the tuple)

Note:

Byte is an integer between 0~255 (2**8-1) (0b11111111, 0b00000000)

The number of strings stored is the number between 1~65535 (2**16-1) (UNICODE16/UNICODE32)

5 Creating a byte string

In non-special cases, single and double quotes have the same effect when creating a string

5.1 Creating an empty byte string

There are two ways of creating an empty string

Mode 1:

>>> B = b"">>> bb">>> b = B" ">>> BB "'

Mode 2:

>>> D = bytes ()>>> Db"
5.2 Creating a non-empty byte string
>>> A = b"ABCD">>> B = b"ABCD">>> C = b" "ABCD" ">>> d = b'\x41\x42'>>>AB'ABCD'>>>BB'ABCD'>>>CB'ABCD'>>>DB'AB'
Construct (create) function for 6-byte string bytes

bytes () : Creates an empty byte string, equivalent to B '
bytes (integer-iterated object) : Initializes a byte string with an iterative object (the value must be an integer of 0~255)
bytes (integer n) : Generates n byte strings with a value of 0
bytes (string, encoding= ' Utf-8 ') : Generates a byte string with the conversion encoding of strings

Example

>>> B = bytes (Range (0x41,0x41+26))>>>BB'abcdefghijklmnopqrstuvwxyz'>>> c = Bytes (4)>>>CB'\x00\x00\x00\x00'>>> e = bytes ("python",'ASCII')>>>EB'python'>>> f = bytes ("English",'Utf-8')>>>FB'\xe4\xb8\xad\xe6\x96\x87'
Operation of 7-byte string

+  +=  *   *=

Comparison operations: < <= > >= = = =!

In/not in operator

Indexed index

Sliced slice

82 Write operations example for a binary file

Python Learning note 015--file Common operations binary (binary file)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.