Trăn mở ()

Access ModesDescriptionrMở một tệp chỉ để đọc. rbMở một tệp chỉ để đọc ở định dạng nhị phân. r+Mở tệp cho cả đọc và ghi. rb+Mở tệp cho cả đọc và ghi ở định dạng nhị phân. wMở một tập tin chỉ để viết. wbMở một tệp chỉ để ghi ở định dạng nhị phân. w+Mở tệp cho cả viết và đọc. wb+Mở tệp cho cả ghi và đọc ở định dạng nhị phân. aMở tệp để nối thêm. abMở tệp để nối thêm ở định dạng nhị phân. a+Mở tệp cho cả việc nối thêm và đọc. ab+Mở tệp cho cả việc nối thêm và đọc ở định dạng nhị phân

Ví dụ sau mở một tệp từ thư mục hiện tại để đọc và ghi vào tệp. Thư mục hiện tại trong Python shell là

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
40

Thí dụ. Mở tệp

Sao chép

>>> f = open["MyFile.txt"] # Open MyFile.txt from the current directory for reading
>>> f = open["MyFile.txt", 'w'] # Open MyFile.txt from the current directory for writing
# perform file operations using f
>>> f.close[]

Các hoạt động của tệp có thể phát sinh ngoại lệ nếu xảy ra sự cố. Vì vậy, nên sử dụng try-ngoại trừ cuối cùng để xử lý các ngoại lệ và đóng một đối tượng tệp

Có một số cách để trình bày đầu ra của một chương trình; . Chương này sẽ thảo luận về một số khả năng

7. 1. Định dạng đầu ra đẹp hơn¶

Cho đến nay, chúng tôi đã gặp hai cách viết giá trị. câu lệnh biểu thức và hàm

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
9. [Cách thứ ba là sử dụng phương thức
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
0 của các đối tượng tệp; tệp đầu ra tiêu chuẩn có thể được tham chiếu là
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
1. Xem Tham khảo thư viện để biết thêm thông tin về điều này. ]

Thông thường, bạn sẽ muốn kiểm soát nhiều hơn định dạng đầu ra của mình hơn là chỉ in các giá trị được phân tách bằng dấu cách. Có một số cách để định dạng đầu ra

  • Để sử dụng các chuỗi ký tự được định dạng , hãy bắt đầu một chuỗi bằng

    >>> import math
    >>> print[f'The value of pi is approximately {math.pi:.3f}.']
    The value of pi is approximately 3.142.
    
    2 hoặc
    >>> import math
    >>> print[f'The value of pi is approximately {math.pi:.3f}.']
    The value of pi is approximately 3.142.
    
    3 trước dấu ngoặc kép mở hoặc dấu ngoặc kép ba. Bên trong chuỗi này, bạn có thể viết một biểu thức Python giữa các ký tự
    >>> import math
    >>> print[f'The value of pi is approximately {math.pi:.3f}.']
    The value of pi is approximately 3.142.
    
    4 và
    >>> import math
    >>> print[f'The value of pi is approximately {math.pi:.3f}.']
    The value of pi is approximately 3.142.
    
    5 có thể tham chiếu đến các biến hoặc giá trị bằng chữ.

    ________số 8

  • Phương pháp chuỗi

    >>> import math
    >>> print[f'The value of pi is approximately {math.pi:.3f}.']
    The value of pi is approximately 3.142.
    
    6 đòi hỏi nhiều nỗ lực thủ công hơn. Bạn vẫn sẽ sử dụng
    >>> import math
    >>> print[f'The value of pi is approximately {math.pi:.3f}.']
    The value of pi is approximately 3.142.
    
    4 và
    >>> import math
    >>> print[f'The value of pi is approximately {math.pi:.3f}.']
    The value of pi is approximately 3.142.
    
    5 để đánh dấu nơi một biến sẽ được thay thế và có thể cung cấp các chỉ thị định dạng chi tiết, nhưng bạn cũng cần cung cấp thông tin cần định dạng

    >>> s = 'Hello, world.'
    >>> str[s]
    'Hello, world.'
    >>> repr[s]
    "'Hello, world.'"
    >>> str[1/7]
    '0.14285714285714285'
    >>> x = 10 * 3.25
    >>> y = 200 * 200
    >>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
    >>> print[s]
    The value of x is 32.5, and y is 40000...
    >>> # The repr[] of a string adds string quotes and backslashes:
    .. hello = 'hello, world\n'
    >>> hellos = repr[hello]
    >>> print[hellos]
    'hello, world\n'
    >>> # The argument to repr[] may be any Python object:
    .. repr[[x, y, ['spam', 'eggs']]]
    "[32.5, 40000, ['spam', 'eggs']]"
    
    2

  • Cuối cùng, bạn có thể tự xử lý tất cả các chuỗi bằng cách sử dụng các thao tác nối và cắt chuỗi để tạo bất kỳ bố cục nào mà bạn có thể tưởng tượng. Loại chuỗi có một số phương thức thực hiện các thao tác hữu ích để đệm chuỗi theo chiều rộng cột nhất định

Khi bạn không cần đầu ra ưa thích mà chỉ muốn hiển thị nhanh một số biến cho mục đích gỡ lỗi, bạn có thể chuyển đổi bất kỳ giá trị nào thành chuỗi bằng các hàm

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
9 hoặc
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
40

Hàm

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
40 có nghĩa là trả về các biểu diễn của các giá trị mà con người khá dễ đọc, trong khi hàm
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
9 có nghĩa là tạo ra các biểu diễn mà trình thông dịch có thể đọc được [hoặc sẽ buộc một
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
43 nếu không có cú pháp tương đương]. Đối với các đối tượng không có đại diện cụ thể cho tiêu dùng của con người,
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
40 sẽ trả về giá trị giống như
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
9. Nhiều giá trị, chẳng hạn như số hoặc cấu trúc như danh sách và từ điển, có cùng cách biểu diễn khi sử dụng một trong hai hàm. Đặc biệt, các chuỗi có hai biểu diễn riêng biệt

Vài ví dụ

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"

Mô-đun

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
46 chứa một lớp
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
47 cung cấp một cách khác để thay thế các giá trị thành chuỗi, sử dụng các trình giữ chỗ như
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
48 và thay thế chúng bằng các giá trị từ một từ điển, nhưng cung cấp ít quyền kiểm soát định dạng hơn nhiều

7. 1. 1. Chuỗi ký tự được định dạng¶

Chuỗi ký tự được định dạng [còn được gọi tắt là f-strings] cho phép bạn bao gồm giá trị của các biểu thức Python bên trong một chuỗi bằng cách thêm tiền tố vào chuỗi là

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
2 hoặc
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
3 và viết biểu thức dưới dạng
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
61.

Một công cụ xác định định dạng tùy chọn có thể theo sau biểu thức. Điều này cho phép kiểm soát tốt hơn cách định dạng giá trị. Ví dụ sau đây làm tròn số pi đến ba vị trí sau dấu thập phân

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.

Truyền một số nguyên sau

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
62 sẽ khiến trường đó có số lượng ký tự tối thiểu. Điều này rất hữu ích để tạo các cột thẳng hàng

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
4

Các công cụ sửa đổi khác có thể được sử dụng để chuyển đổi giá trị trước khi nó được định dạng.

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
63 áp dụng
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
64,
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
65 áp dụng
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
40 và
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
67 áp dụng
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
9

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
6

Trình xác định

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
69 có thể được sử dụng để mở rộng một biểu thức thành văn bản của biểu thức, một dấu bằng, sau đó biểu diễn biểu thức được đánh giá

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
8

Xem biểu thức tự ghi tài liệu để biết thêm thông tin về trình xác định

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
69. Để tham khảo về các đặc tả định dạng này, hãy xem hướng dẫn tham khảo về Ngôn ngữ nhỏ đặc tả định dạng .

7. 1. 2. Phương thức String format[]¶

Cách sử dụng cơ bản của phương pháp

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
6 trông như thế này

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
1

Các dấu ngoặc và ký tự bên trong chúng [được gọi là các trường định dạng] được thay thế bằng các đối tượng được truyền vào phương thức

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
6. Một số trong ngoặc có thể được sử dụng để chỉ vị trí của đối tượng được truyền vào phương thức
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
6

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
4

Nếu các đối số từ khóa được sử dụng trong phương thức

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
6, giá trị của chúng được gọi bằng cách sử dụng tên của đối số

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
6

Đối số vị trí và từ khóa có thể được kết hợp tùy ý

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
20

Nếu bạn có một chuỗi định dạng thực sự dài mà bạn không muốn chia nhỏ, sẽ rất tuyệt nếu bạn có thể tham chiếu các biến được định dạng theo tên thay vì theo vị trí. Điều này có thể được thực hiện bằng cách chuyển chính tả và sử dụng dấu ngoặc vuông

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
85 để truy cập các phím

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
21

Điều này cũng có thể được thực hiện bằng cách chuyển từ điển

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
86 làm đối số từ khóa với ký hiệu
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
87

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
22

Điều này đặc biệt hữu ích khi kết hợp với hàm tích hợp sẵn

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
88, trả về một từ điển chứa tất cả các biến cục bộ

Ví dụ: các dòng sau tạo ra một tập hợp các cột được sắp xếp gọn gàng cho các số nguyên và hình vuông và hình khối của chúng

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
23

Để biết tổng quan đầy đủ về định dạng chuỗi với

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
6, hãy xem Cú pháp định dạng chuỗi .

7. 1. 3. Định dạng chuỗi thủ công¶

Đây là cùng một bảng hình vuông và hình khối, được định dạng thủ công

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
24

[Lưu ý rằng một khoảng trắng giữa mỗi cột đã được thêm vào theo cách hoạt động của

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
9. nó luôn thêm khoảng trắng giữa các đối số của nó. ]

Phương thức

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
11 của các đối tượng chuỗi canh phải một chuỗi trong một trường có độ rộng nhất định bằng cách thêm vào đó các khoảng trắng ở bên trái. Có các phương pháp tương tự
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
12 và
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
13. Các phương thức này không viết bất cứ thứ gì, chúng chỉ trả về một chuỗi mới. Nếu chuỗi đầu vào quá dài, họ sẽ không cắt bớt mà trả về không thay đổi; . [Nếu bạn thực sự muốn cắt bớt, bạn luôn có thể thêm thao tác cắt lát, như trong
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
14. ]

Có một phương pháp khác,

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
15, đệm một chuỗi số ở bên trái bằng các số không. Nó hiểu về dấu cộng và dấu trừ

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
25

7. 1. 4. Định dạng chuỗi cũ¶

Toán tử % [modulo] cũng có thể được sử dụng để định dạng chuỗi. Cho trước

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
16, các trường hợp của
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
17 trong
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
46 được thay thế bằng 0 hoặc nhiều phần tử của
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
19. Hoạt động này thường được gọi là nội suy chuỗi. Ví dụ

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
26

Bạn có thể tìm thêm thông tin trong phần Định dạng chuỗi kiểu printf .

7. 2. Đọc và ghi tệp¶

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
40 trả về một đối tượng tệp và được sử dụng phổ biến nhất với hai đối số vị trí và một đối số từ khóa.
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
41

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
27

Đối số đầu tiên là một chuỗi chứa tên tệp. Đối số thứ hai là một chuỗi khác chứa một vài ký tự mô tả cách sử dụng tệp. chế độ có thể là

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
42 khi tệp sẽ chỉ được đọc,
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
43 chỉ để ghi [tệp hiện có cùng tên sẽ bị xóa] và
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
44 mở tệp để nối thêm; .
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
45 mở tệp cho cả đọc và viết. Đối số chế độ là tùy chọn;

Thông thường, các tệp được mở ở chế độ văn bản, nghĩa là bạn đọc và ghi các chuỗi từ và đến tệp, được mã hóa bằng một mã hóa cụ thể. Nếu mã hóa không được chỉ định, mặc định sẽ phụ thuộc vào nền tảng [xem

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
40]. Bởi vì UTF-8 là tiêu chuẩn thực tế hiện đại, nên sử dụng
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
48 trừ khi bạn biết rằng mình cần sử dụng một mã hóa khác. Thêm một
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
49 vào chế độ sẽ mở tệp ở chế độ nhị phân. Dữ liệu chế độ nhị phân được đọc và ghi dưới dạng đối tượng
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
60. Bạn không thể chỉ định mã hóa khi mở tệp ở chế độ nhị phân

Ở chế độ văn bản, mặc định khi đọc là chuyển đổi kết thúc dòng dành riêng cho nền tảng [

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
61 trên Unix,
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
62 trên Windows] thành chỉ
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
61. Khi viết ở chế độ văn bản, mặc định là chuyển đổi các lần xuất hiện của
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
61 trở lại kết thúc dòng dành riêng cho nền tảng. Việc sửa đổi hậu trường này đối với dữ liệu tệp phù hợp với tệp văn bản, nhưng sẽ làm hỏng dữ liệu nhị phân như vậy trong tệp
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
65 hoặc
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
66. Hãy thật cẩn thận khi sử dụng chế độ nhị phân khi đọc và ghi các tệp đó

Bạn nên sử dụng từ khóa

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
67 khi xử lý các đối tượng tệp. Ưu điểm là tệp được đóng đúng cách sau khi bộ phần mềm của nó kết thúc, ngay cả khi một ngoại lệ được đưa ra tại một số điểm. Sử dụng
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
67 cũng ngắn hơn nhiều so với cách viết tương đương
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
69-
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
200 khối

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
28

Nếu bạn không sử dụng từ khóa

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
67, thì bạn nên gọi
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
202 để đóng tệp và ngay lập tức giải phóng mọi tài nguyên hệ thống được sử dụng bởi nó

Cảnh báo

Gọi

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
203 mà không sử dụng từ khóa
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
67 hoặc gọi
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
202 có thể dẫn đến các đối số của
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
203 không được ghi hoàn toàn vào đĩa, ngay cả khi chương trình thoát thành công

Sau khi một đối tượng tệp bị đóng, bằng câu lệnh

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
67 hoặc bằng cách gọi
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
202, các nỗ lực sử dụng đối tượng tệp sẽ tự động thất bại

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
29

7. 2. 1. Phương thức của đối tượng tệp¶

Phần còn lại của các ví dụ trong phần này sẽ giả định rằng một đối tượng tệp có tên là

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
2 đã được tạo

Để đọc nội dung của tệp, hãy gọi

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
210, đọc một số lượng dữ liệu và trả về dưới dạng chuỗi [ở chế độ văn bản] hoặc đối tượng byte [ở chế độ nhị phân]. kích thước là một đối số số tùy chọn. Khi kích thước bị bỏ qua hoặc âm, toàn bộ nội dung của tệp sẽ được đọc và trả về; . Mặt khác, ở hầu hết các ký tự kích thước [ở chế độ văn bản] hoặc byte kích thước [ở chế độ nhị phân] đều được đọc và trả về. Nếu đã đến cuối tệp,
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
211 sẽ trả về một chuỗi rỗng [
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
212]

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
0

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
213 đọc một dòng từ tệp; . Điều này làm cho giá trị trả về trở nên rõ ràng;

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
1

Để đọc các dòng từ một tệp, bạn có thể lặp qua đối tượng tệp. Đây là bộ nhớ hiệu quả, nhanh chóng và dẫn đến mã đơn giản

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
2

Nếu bạn muốn đọc tất cả các dòng của tệp trong danh sách, bạn cũng có thể sử dụng

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
217 hoặc
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
218

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
219 ghi nội dung của chuỗi vào tệp, trả về số ký tự đã ghi

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
3

Các loại đối tượng khác cần được chuyển đổi – thành chuỗi [ở chế độ văn bản] hoặc đối tượng byte [ở chế độ nhị phân] – trước khi viết chúng

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
4

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
220 trả về một số nguyên cho biết vị trí hiện tại của đối tượng tệp trong tệp được biểu thị dưới dạng số byte từ đầu tệp khi ở chế độ nhị phân và một số mờ khi ở chế độ văn bản

Để thay đổi vị trí của đối tượng tệp, hãy sử dụng

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
221. Vị trí được tính từ việc thêm phần bù vào điểm tham chiếu; . Giá trị bắt nguồn từ 0 đo từ đầu tệp, 1 sử dụng vị trí tệp hiện tại và 2 sử dụng cuối tệp làm điểm tham chiếu. từ đâu có thể được bỏ qua và mặc định là 0, sử dụng phần đầu của tệp làm điểm tham chiếu

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
5

Trong các tệp văn bản [những tệp được mở mà không có

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
222 trong chuỗi chế độ], chỉ cho phép tìm kiếm liên quan đến phần đầu của tệp [ngoại lệ là tìm kiếm đến chính tệp kết thúc bằng
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
223] và các giá trị bù trừ hợp lệ duy nhất là các giá trị được trả về từ . Bất kỳ giá trị bù nào khác đều tạo ra hành vi không xác định

Các đối tượng tệp có một số phương thức bổ sung, chẳng hạn như

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
225 và
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
226 ít được sử dụng hơn;

7. 2. 2. Lưu dữ liệu có cấu trúc với
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
227¶

Các chuỗi có thể dễ dàng được ghi và đọc từ một tệp. Các số tốn nhiều công sức hơn một chút, vì phương thức

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
228 chỉ trả về các chuỗi, chuỗi này sẽ phải được chuyển đến một hàm như
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
229, hàm này nhận một chuỗi như
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
230 và trả về giá trị số 123 của nó. Khi bạn muốn lưu các loại dữ liệu phức tạp hơn như danh sách lồng nhau và từ điển, việc phân tích cú pháp và tuần tự hóa bằng tay trở nên phức tạp

Thay vì yêu cầu người dùng liên tục viết và gỡ lỗi mã để lưu các loại dữ liệu phức tạp vào tệp, Python cho phép bạn sử dụng định dạng trao đổi dữ liệu phổ biến được gọi là JSON [Ký hiệu đối tượng JavaScript]. Mô-đun tiêu chuẩn có tên là

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
227 có thể lấy cấu trúc phân cấp dữ liệu Python và chuyển đổi chúng thành biểu diễn chuỗi; . Tái tạo lại dữ liệu từ biểu diễn chuỗi được gọi là giải tuần tự hóa. Giữa tuần tự hóa và giải tuần tự hóa, chuỗi đại diện cho đối tượng có thể đã được lưu trữ trong một tệp hoặc dữ liệu hoặc được gửi qua kết nối mạng tới một số máy ở xa

Ghi chú

Định dạng JSON thường được các ứng dụng hiện đại sử dụng để cho phép trao đổi dữ liệu. Nhiều lập trình viên đã quen thuộc với nó, điều này làm cho nó trở thành một lựa chọn tốt cho khả năng tương tác

Nếu bạn có một đối tượng

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
232, bạn có thể xem biểu diễn chuỗi JSON của nó bằng một dòng mã đơn giản

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
6

Một biến thể khác của hàm

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
233, được gọi là
>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
234, chỉ cần tuần tự hóa đối tượng thành tệp văn bản . Vì vậy, nếu
>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
2 là một đối tượng tệp văn bản được mở để ghi, chúng ta có thể thực hiện việc này.

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
7

Để giải mã lại đối tượng, nếu

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
2 là tệp nhị phân hoặc tệp văn bản object which has been opened for reading:

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
8

Ghi chú

Các tệp JSON phải được mã hóa bằng UTF-8. Sử dụng

>>> import math
>>> print[f'The value of pi is approximately {math.pi:.3f}.']
The value of pi is approximately 3.142.
48 khi mở tệp JSON dưới dạng tệp văn bản cho cả đọc và viết.

Kỹ thuật tuần tự hóa đơn giản này có thể xử lý các danh sách và từ điển, nhưng việc tuần tự hóa các cá thể lớp tùy ý trong JSON đòi hỏi một chút nỗ lực. Tài liệu tham khảo cho mô-đun

>>> s = 'Hello, world.'
>>> str[s]
'Hello, world.'
>>> repr[s]
"'Hello, world.'"
>>> str[1/7]
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr[x] + ', and y is ' + repr[y] + '...'
>>> print[s]
The value of x is 32.5, and y is 40000...
>>> # The repr[] of a string adds string quotes and backslashes:
.. hello = 'hello, world\n'
>>> hellos = repr[hello]
>>> print[hellos]
'hello, world\n'
>>> # The argument to repr[] may be any Python object:
.. repr[[x, y, ['spam', 'eggs']]]
"[32.5, 40000, ['spam', 'eggs']]"
227 có giải thích về điều này

Phương thức open[] là gì?

open[] Phương thức XMLHttpRequest open[] khởi tạo một yêu cầu mới được tạo hoặc khởi tạo lại một yêu cầu hiện có . Ghi chú. Gọi phương thức này cho một yêu cầu đã hoạt động [một yêu cầu open[] đã được gọi] tương đương với việc gọi abort[].

Cú pháp đúng của hàm open[] là gì?

Cú pháp đúng của hàm open[] là gì? . tên_tệp. Đối số tên_tệp là một giá trị chuỗi chứa tên của tệp mà bạn muốn truy cập. chế độ truy cập. access_mode xác định chế độ trong đó tệp phải được mở, tôi. e. , đọc, viết, chắp thêm, v.v.

Có open[] Tạo một tệp Python không?

Hàm open[], như đã đề cập trước đó, tạo đối tượng xử lý tệp để mở hoặc tạo tệp tại đường dẫn đã chỉ định . Chức năng này có một vài chế độ khác nhau mà chúng ta có thể sử dụng. Chế độ r được sử dụng để mở tệp ở chế độ chỉ đọc.

Hai đối số được lấy bởi hàm open[] là gì?

Người dùng sử dụng hàm open[] với hai đối số. tên tệp và chế độ [đọc hoặc ghi] để trả về một đối tượng tệp.

Chủ Đề