Chức năng Textwrap trong Python là gì?

Gói văn bản là một công cụ hữu ích khi xử lý ngôn ngữ văn bản trong các ứng dụng xử lý ngôn ngữ tự nhiên, phân tích dữ liệu và thậm chí cả tác phẩm nghệ thuật hoặc thiết kế. Mô-đun textwrap trong Python cung cấp các chức năng thuận tiện để giúp tạo hiệu ứng bao bọc văn bản

Mục lục

Mô-đun này cũng cung cấp một số chức năng liên quan có thể được sử dụng để chuẩn hóa khoảng trắng, loại bỏ hoặc thêm thụt lề, văn bản tiền tố và hậu tố và vô số cách sử dụng khác. Ngoài ra, mô-đun

import statistics

# Calculate the average line length
avg_length = statistics.mean[[len[x] for x in text.splitlines[]]]

>>> 48.083333333333336
1 có lớp
import statistics

# Calculate the average line length
avg_length = statistics.mean[[len[x] for x in text.splitlines[]]]

>>> 48.083333333333336
2 cung cấp chức năng tương tự với sự cân nhắc hiệu quả hơn về bộ nhớ

Gói văn bản trong Python

Các ví dụ trong bài viết này sử dụng văn bản từ Bá tước Monte Cristo của Alexander Dumas, hiện là một phần của phạm vi công cộng và có sẵn miễn phí từ Project Gutenberg ở một số định dạng. Các văn bản sau đây xuất phát từ ba đoạn đầu tiên của chương đầu tiên

On the 24th of February, 1815, the look-out at Notre-Dame de la Garde
signalled the three-master, the _Pharaon_ from Smyrna, Trieste, and
Naples.

As usual, a pilot put off immediately, and rounding the Château d’If,
got on board the vessel between Cape Morgiou and Rion island.

Immediately, and according to custom, the ramparts of Fort Saint-Jean
were covered with spectators; it is always an event at Marseilles for a
ship to come into port, especially when this ship, like the _Pharaon_,
has been built, rigged, and laden at the old Phocee docks, and belongs
to an owner of the city.

Sử dụng đoạn mã sau, tôi nhanh chóng biết được độ dài dòng trung bình của văn bản này

import statistics

# Calculate the average line length
avg_length = statistics.mean[[len[x] for x in text.splitlines[]]]

>>> 48.083333333333336

Điều này không phù hợp với bất cứ điều gì tôi đang làm nhưng sẽ giúp hướng dẫn các giá trị được chuyển cho chiều rộng trong trình bao bọc để minh họa trực quan hơn. Rốt cuộc, hiệu ứng của việc gói một chuỗi văn bản rộng 48 ký tự thành 50 ký tự sẽ không ấn tượng lắm về mặt hình ảnh

Chức năng độc lập vs. Lớp TextWrapper

Mô-đun textwrap trong Python cung cấp một loạt phương thức dưới dạng hàm độc lập hoặc dưới dạng phương thức lớp thông qua lớp

import statistics

# Calculate the average line length
avg_length = statistics.mean[[len[x] for x in text.splitlines[]]]

>>> 48.083333333333336
2. Tài liệu chính thức tư vấn cho chúng tôi về lớp
import statistics

# Calculate the average line length
avg_length = statistics.mean[[len[x] for x in text.splitlines[]]]

>>> 48.083333333333336
2 như vậy

” Nếu bạn chỉ gói hoặc điền một hoặc hai chuỗi văn bản, các chức năng tiện lợi sẽ đủ tốt; . ”

Nói cách khác, không cần phải ngốn bộ nhớ nếu bạn chỉ chỉnh sửa một hoặc hai câu. Chúng ta sẽ xem xét từng kiểu sử dụng này bắt đầu với lớp TextWrapper vì một số thuộc tính thể hiện được xác định có liên quan đến các hàm độc lập

Chức năng độc lập

Các chức năng sau được cung cấp dưới dạng độc lập và cung cấp các tiện ích văn bản hữu ích. Trái ngược với ý nghĩa của tên mô-đun, các hàm này giải quyết nhiều trường hợp sử dụng phổ biến ngoài việc gói các dòng văn bản trong python theo độ dài cố định. Tuy nhiên, đó là nơi chúng ta sẽ bắt đầu

bọc

“Trả về danh sách các dòng đầu ra, không có dòng mới cuối cùng. Nếu đầu ra được bao bọc không có nội dung, danh sách trả về trống. ” — Tài liệu TextWrapper

Hàm này cung cấp tiện ích đặc trưng nhất của mô-đun textwrap—bằng cách ngắt dòng văn bản thành một số lượng ký tự hạn chế. Hàm này chấp nhận một chuỗi văn bản hoặc một tập hợp các văn bản, đối số chiều rộng và bất kỳ đối số thể hiện nào khác có sẵn cho lớp TextWrapper

Sử dụng văn bản mẫu, hàm ngắt dòng và đối số chiều rộng là 25 sẽ tạo ra kết quả sau

# Wrap text to lines of 25 characters
textwrap.wrap[text=sample_text, width=25]

# The result is a list of 25-character long strings [last item may be shorter]
>>> ['On the 24th of February,', '1815, the look-out at', 'Notre-Dame de la Garde', 'signalled the three-', 'master, the _Pharaon_', 'from Smyrna, Trieste, and', 'Naples.  As usual, a', 'pilot put off', 'immediately, and rounding', 'the Château d’If, got on', 'board the vessel between', 'Cape Morgiou and Rion', 'island.  Immediately, and', 'according to custom, the', 'ramparts of Fort Saint-', 'Jean were covered with', 'spectators; it is always', 'an event at Marseilles', 'for a ship to come into', 'port, especially when', 'this ship, like the', '_Pharaon_, has been', 'built, rigged, and laden', 'at the old Phocee docks,', 'and belongs to an owner', 'of the city.']

lấp đầy

“Bao bọc một đoạn văn bản trong văn bản và trả về một chuỗi đơn chứa đoạn văn được bao bọc” — Tài liệu TextWrapper

Sau khi đã sử dụng hàm bọc, người ta có thể xem xét tiện ích của một tập hợp các chuỗi con. Tôi thường thấy trường hợp sử dụng cuối cùng của mình dành cho khối văn bản thường xuyên hơn là tập hợp các chuỗi con

Nói cách khác, tôi muốn việc ngắt dòng văn bản dẫn đến một khối văn bản có độ dài dòng trung bình ngắn hơn. Tôi có xu hướng sử dụng thứ gì đó dọc theo dòng

import statistics

# Calculate the average line length
avg_length = statistics.mean[[len[x] for x in text.splitlines[]]]

>>> 48.083333333333336
5 để hoàn thành mục tiêu này—đó chính xác là chức năng của hàm
import statistics

# Calculate the average line length
avg_length = statistics.mean[[len[x] for x in text.splitlines[]]]

>>> 48.083333333333336
6

# Create a wrap of 25-characters width
textwrap.fill[text=text, width=25]

# Resulting Text
On the 24th of February,
1815, the look-out at
Notre-Dame de la Garde
signalled the three-
master, the _Pharaon_
from Smyrna, Trieste, and
Naples.  As usual, a
pilot put off
immediately, and rounding
the Château d’If, got on
board the vessel between
Cape Morgiou and Rion
island.  Immediately, and
according to custom, the
ramparts of Fort Saint-
Jean were covered with
spectators; it is always
an event at Marseilles
for a ship to come into
port, especially when
this ship, like the
_Pharaon_, has been
built, rigged, and laden
at the old Phocee docks,
and belongs to an owner
of the city.

Ghi chú. Hàm điền có các đối số giống như hàm bọc. Sự khác biệt duy nhất là nó nối các dòng kết quả thành một khối văn bản duy nhất

rút ngắn

“Thu gọn và cắt bớt văn bản đã cho để vừa với chiều rộng đã cho. ” — Tài liệu TextWrapper

Hàm

import statistics

# Calculate the average line length
avg_length = statistics.mean[[len[x] for x in text.splitlines[]]]

>>> 48.083333333333336
7 có thể được coi là hàm tạo đoạn trích. Nó lấy một chuỗi làm đối số và cắt ngắn chuỗi đã nói thành một số ký tự cụ thể. Trong quá trình cắt ngắn, chức năng rút ngắn cũng chuẩn hóa khoảng trắng sao cho văn bản kết quả chỉ chứa các ký tự khoảng trắng đơn lẻ. Kiểm tra bảng tra cứu ASCII này để tham khảo về các giá trị ký tự khoảng trắng

Hàm

import statistics

# Calculate the average line length
avg_length = statistics.mean[[len[x] for x in text.splitlines[]]]

>>> 48.083333333333336
7 là ví dụ đầu tiên về các hàm độc lập trong đó các đối số từ khóa khác với
import statistics

# Calculate the average line length
avg_length = statistics.mean[[len[x] for x in text.splitlines[]]]

>>> 48.083333333333336
9 và
# Wrap text to lines of 25 characters
textwrap.wrap[text=sample_text, width=25]

# The result is a list of 25-character long strings [last item may be shorter]
>>> ['On the 24th of February,', '1815, the look-out at', 'Notre-Dame de la Garde', 'signalled the three-', 'master, the _Pharaon_', 'from Smyrna, Trieste, and', 'Naples.  As usual, a', 'pilot put off', 'immediately, and rounding', 'the Château d’If, got on', 'board the vessel between', 'Cape Morgiou and Rion', 'island.  Immediately, and', 'according to custom, the', 'ramparts of Fort Saint-', 'Jean were covered with', 'spectators; it is always', 'an event at Marseilles', 'for a ship to come into', 'port, especially when', 'this ship, like the', '_Pharaon_, has been', 'built, rigged, and laden', 'at the old Phocee docks,', 'and belongs to an owner', 'of the city.']
0 rất hữu ích. Sử dụng đối số
# Wrap text to lines of 25 characters
textwrap.wrap[text=sample_text, width=25]

# The result is a list of 25-character long strings [last item may be shorter]
>>> ['On the 24th of February,', '1815, the look-out at', 'Notre-Dame de la Garde', 'signalled the three-', 'master, the _Pharaon_', 'from Smyrna, Trieste, and', 'Naples.  As usual, a', 'pilot put off', 'immediately, and rounding', 'the Château d’If, got on', 'board the vessel between', 'Cape Morgiou and Rion', 'island.  Immediately, and', 'according to custom, the', 'ramparts of Fort Saint-', 'Jean were covered with', 'spectators; it is always', 'an event at Marseilles', 'for a ship to come into', 'port, especially when', 'this ship, like the', '_Pharaon_, has been', 'built, rigged, and laden', 'at the old Phocee docks,', 'and belongs to an owner', 'of the city.']
1 ở đây chỉ định những ký tự nào sẽ được thêm vào văn bản kết quả. Cá nhân tôi thấy một hình elip […] phù hợp về mặt trực quan như trong ví dụ sau

# Use the shorten function to create a concatenated string
textwrap.shorten[text=sample_text, width=75, placeholder="..."]

# The resulting concatenated text with the ellipsis added
# via the "placeholder" argument.
>>> On the 24th of February, 1815, the look-out at Notre-Dame de la Garde...

Ghi chú. Nếu việc chuẩn hóa khoảng trắng dẫn đến văn bản ngắn hơn chiều rộng đã chỉ định thì văn bản đó sẽ được trả về nguyên vẹn. Văn bản toàn khoảng trắng sẽ trả về một chuỗi trống và cũng không thêm trình giữ chỗ

vết lõm

“Xóa mọi khoảng trắng ở đầu phổ biến khỏi mọi dòng trong văn bản. ” — Tài liệu TextWrapper

Chức năng dedent mất một chút thời gian để quấn quanh đầu tôi. Mục đích của nó là chuẩn hóa khoảng trắng ở đầu cho toàn bộ khối văn bản — không phải các dòng riêng lẻ. Trường hợp phổ biến ở đây là dành cho văn bản dự kiến ​​được tìm thấy trong những thứ như tài liệu phương pháp, định dạng chuỗi, v.v. Hàm này sẽ không hoạt động với văn bản mẫu hiện tại nhưng hãy xem xét trường hợp sau đây của chuỗi tài liệu được thụt lề từ mã nguồn đã được xác định

def dedent[text]:
    """Remove any common leading whitespace from every line in `text`.

    This can be used to make triple-quoted strings line up with the left
    edge of the display, while still presenting them in the source code
    in indented form.

    Note that tabs and spaces are both treated as whitespace, but they
    are not equal: the lines "  hello" and "\\thello" are
    considered to have no common leading whitespace.

    Entirely blank lines are normalized to a newline character.
    """
    ...

Sao chép/dán văn bản chuỗi tài liệu được thụt lề này sẽ dẫn đến các dòng văn bản có chung một chuỗi 4 ký tự dấu cách đơn [KHÔNG PHẢI LÀ TAB. ] Loại bỏ chỉ định này theo cách thủ công là đủ dễ dàng và không chứng minh được sự tồn tại của hàm phụ thuộc

Hãy tưởng tượng bằng cách sử dụng chuỗi tài liệu của mọi chức năng trong thư viện chuẩn [có thể để phân tích NLP] theo cách yêu cầu loại bỏ các chỉ định. Loại bỏ thủ công sẽ khó khăn về mặt chức năng. Sử dụng hàm dedent sẽ đảm bảo rằng tất cả các indent đã được loại bỏ trong quá trình tiền xử lý. Một ví dụ nhanh

# Another excerpt of our sample text, with poorly formatted entry
# using blank lines and indendation
string = """
    On the 24th of February, 1815, the look-out at Notre-Dame de la Garde
    signalled the three-master, the _Pharaon_ from Smyrna, Trieste, and
    Naples.
    """
# Dedent the text
textwrap.dedent[text=string]
>>> 

On the 24th of February, 1815, the look-out at Notre-Dame de la Garde
signalled the three-master, the _Pharaon_ from Smyrna, Trieste, and
Naples.

Lưu ý mẫu tác phẩm này của Dumas có một ký tự dòng mới ở đầu và cuối không bị xóa. Ký tự xuống dòng không chung cho tất cả các dòng văn bản do đó không bị xóa trong thuật toán hàm dedent. Chỉ khoảng cách thụt vào được chia sẻ bởi tất cả các dòng bị xóa

thụt vào

“Thêm tiền tố vào đầu dòng đã chọn trong văn bản. ” — Tài liệu TextWrapper

Chức năng thụt lề hoạt động như người ta mong đợi; . Xem xét kết quả của việc sử dụng chức năng thụt lề trên văn bản mẫu ban đầu của chúng tôi

# Prefix text lines with three hyphens and a space
textwrap.indent[text=sample_text, prefix="--- "]

>>>
--- On the 24th of February, 1815, the look-out at Notre-Dame de la Garde
--- signalled the three-master, the _Pharaon_ from Smyrna, Trieste, and
--- Naples.

--- As usual, a pilot put off immediately, and rounding the Château d’If,
--- got on board the vessel between Cape Morgiou and Rion island.

--- Immediately, and according to custom, the ramparts of Fort Saint-Jean
--- were covered with spectators; it is always an event at Marseilles for a
--- ship to come into port, especially when this ship, like the _Pharaon_,
--- has been built, rigged, and laden at the old Phocee docks, and belongs
--- to an owner of the city.

Theo mặc định, hàm thụt lề sẽ thêm giá trị của tiền tố vào bất kỳ dòng nào ngoài các dòng chỉ có khoảng trắng. Nói cách khác, tiền tố không được thêm vào các dòng trống giữa các đoạn trong văn bản mẫu của chúng tôi

Hàm thụt lề cũng cho phép một tham số có thể gọi được chuyển vào dưới dạng đối số

# Wrap text to lines of 25 characters
textwrap.wrap[text=sample_text, width=25]

# The result is a list of 25-character long strings [last item may be shorter]
>>> ['On the 24th of February,', '1815, the look-out at', 'Notre-Dame de la Garde', 'signalled the three-', 'master, the _Pharaon_', 'from Smyrna, Trieste, and', 'Naples.  As usual, a', 'pilot put off', 'immediately, and rounding', 'the Château d’If, got on', 'board the vessel between', 'Cape Morgiou and Rion', 'island.  Immediately, and', 'according to custom, the', 'ramparts of Fort Saint-', 'Jean were covered with', 'spectators; it is always', 'an event at Marseilles', 'for a ship to come into', 'port, especially when', 'this ship, like the', '_Pharaon_, has been', 'built, rigged, and laden', 'at the old Phocee docks,', 'and belongs to an owner', 'of the city.']
2. Điều này nhằm cung cấp khả năng kiểm soát logic cho những dòng nào được cung cấp
# Wrap text to lines of 25 characters
textwrap.wrap[text=sample_text, width=25]

# The result is a list of 25-character long strings [last item may be shorter]
>>> ['On the 24th of February,', '1815, the look-out at', 'Notre-Dame de la Garde', 'signalled the three-', 'master, the _Pharaon_', 'from Smyrna, Trieste, and', 'Naples.  As usual, a', 'pilot put off', 'immediately, and rounding', 'the Château d’If, got on', 'board the vessel between', 'Cape Morgiou and Rion', 'island.  Immediately, and', 'according to custom, the', 'ramparts of Fort Saint-', 'Jean were covered with', 'spectators; it is always', 'an event at Marseilles', 'for a ship to come into', 'port, especially when', 'this ship, like the', '_Pharaon_, has been', 'built, rigged, and laden', 'at the old Phocee docks,', 'and belongs to an owner', 'of the city.']
3. Ví dụ: nếu một người nhấn mạnh vào tiền tố các dòng trống thì cú pháp sau đây có thể được sử dụng

# Add three hyphens and space as prefix to ALL lines including blank lines
textwrap.indent[text=sample_text, prefix='--- ', predicate=lambda line: True]

>>>
--- On the 24th of February, 1815, the look-out at Notre-Dame de la Garde
--- signalled the three-master, the _Pharaon_ from Smyrna, Trieste, and
--- Naples.
--- 
--- As usual, a pilot put off immediately, and rounding the Château d’If,
--- got on board the vessel between Cape Morgiou and Rion island.
--- 
--- Immediately, and according to custom, the ramparts of Fort Saint-Jean
--- were covered with spectators; it is always an event at Marseilles for a
--- ship to come into port, especially when this ship, like the _Pharaon_,
--- has been built, rigged, and laden at the old Phocee docks, and belongs
--- to an owner of the city.

Các cách tiếp cận sáng tạo khác có thể dành cho những trường hợp như vậy khi một dòng chứa một loạt ký tự mà người ta muốn có thông báo trực quan. Ví dụ: mỗi dòng chứa từ “and”

# Add a leading asterisk to any line containing the word 'and'
textwrap.indent[text=sample_text, prefix='* ', predicate=lambda line: 'and' in line]

>>>
On the 24th of February, 1815, the look-out at Notre-Dame de la Garde
* signalled the three-master, the _Pharaon_ from Smyrna, Trieste, and
Naples.

* As usual, a pilot put off immediately, and rounding the Château d’If,
* got on board the vessel between Cape Morgiou and Rion island.

* Immediately, and according to custom, the ramparts of Fort Saint-Jean
were covered with spectators; it is always an event at Marseilles for a
ship to come into port, especially when this ship, like the _Pharaon_,
* has been built, rigged, and laden at the old Phocee docks, and belongs
to an owner of the city.

Một phần mở rộng khả thi của chức năng này mà tôi muốn thấy trong tương lai là hỗ trợ lựa chọn có điều kiện giữa nhiều tiền tố. Ví dụ: thêm một chuỗi 4 dấu cách để thụt lề mỗi dòng không có từ và hoặc một chuỗi 3 dấu cách có dấu hoa thị ở đầu nếu có. Hiện tại, một người sẽ được yêu cầu làm một cái gì đó tương tự như thế này

import statistics

# Calculate the average line length
avg_length = statistics.mean[[len[x] for x in text.splitlines[]]]

>>> 48.083333333333336
0

Kết quả cuối cùng ở đây là một biểu diễn cân bằng trực quan hơn về dấu * biểu thị việc bao gồm từ và trong một dòng từ văn bản mẫu của chúng tôi

Lớp TextWrapper

Lớp TextWrapper sẽ được sử dụng cho các phần văn bản lớn hơn trong đó hiệu quả bộ nhớ có thể phù hợp. Ví dụ: kho văn bản 1 triệu từ đang được xử lý trước để phân tích NLP. Các đối số từ khóa được truyền trong quá trình khởi tạo có sẵn cho các đối tượng lớp dưới dạng thuộc tính thể hiện. Lớp TextWrapper cũng cung cấp

# Wrap text to lines of 25 characters
textwrap.wrap[text=sample_text, width=25]

# The result is a list of 25-character long strings [last item may be shorter]
>>> ['On the 24th of February,', '1815, the look-out at', 'Notre-Dame de la Garde', 'signalled the three-', 'master, the _Pharaon_', 'from Smyrna, Trieste, and', 'Naples.  As usual, a', 'pilot put off', 'immediately, and rounding', 'the Château d’If, got on', 'board the vessel between', 'Cape Morgiou and Rion', 'island.  Immediately, and', 'according to custom, the', 'ramparts of Fort Saint-', 'Jean were covered with', 'spectators; it is always', 'an event at Marseilles', 'for a ship to come into', 'port, especially when', 'this ship, like the', '_Pharaon_, has been', 'built, rigged, and laden', 'at the old Phocee docks,', 'and belongs to an owner', 'of the city.']
4 và
import statistics

# Calculate the average line length
avg_length = statistics.mean[[len[x] for x in text.splitlines[]]]

>>> 48.083333333333336
6 dưới dạng các phương thức công khai

Suy nghĩ cuối cùng

Mô-đun

import statistics

# Calculate the average line length
avg_length = statistics.mean[[len[x] for x in text.splitlines[]]]

>>> 48.083333333333336
1 chứa một số công cụ thao tác văn bản khá cơ bản. Rất hiếm khi tôi thấy rằng một dự án dựa trên văn bản có thể được giải quyết đầy đủ thông qua mô-đun này. Tuy nhiên, các hàm
# Wrap text to lines of 25 characters
textwrap.wrap[text=sample_text, width=25]

# The result is a list of 25-character long strings [last item may be shorter]
>>> ['On the 24th of February,', '1815, the look-out at', 'Notre-Dame de la Garde', 'signalled the three-', 'master, the _Pharaon_', 'from Smyrna, Trieste, and', 'Naples.  As usual, a', 'pilot put off', 'immediately, and rounding', 'the Château d’If, got on', 'board the vessel between', 'Cape Morgiou and Rion', 'island.  Immediately, and', 'according to custom, the', 'ramparts of Fort Saint-', 'Jean were covered with', 'spectators; it is always', 'an event at Marseilles', 'for a ship to come into', 'port, especially when', 'this ship, like the', '_Pharaon_, has been', 'built, rigged, and laden', 'at the old Phocee docks,', 'and belongs to an owner', 'of the city.']
4 và
# Wrap text to lines of 25 characters
textwrap.wrap[text=sample_text, width=25]

# The result is a list of 25-character long strings [last item may be shorter]
>>> ['On the 24th of February,', '1815, the look-out at', 'Notre-Dame de la Garde', 'signalled the three-', 'master, the _Pharaon_', 'from Smyrna, Trieste, and', 'Naples.  As usual, a', 'pilot put off', 'immediately, and rounding', 'the Château d’If, got on', 'board the vessel between', 'Cape Morgiou and Rion', 'island.  Immediately, and', 'according to custom, the', 'ramparts of Fort Saint-', 'Jean were covered with', 'spectators; it is always', 'an event at Marseilles', 'for a ship to come into', 'port, especially when', 'this ship, like the', '_Pharaon_, has been', 'built, rigged, and laden', 'at the old Phocee docks,', 'and belongs to an owner', 'of the city.']
8 của nó đều rất hữu ích và mang lại lợi ích khi được đưa vào thư viện Python tiêu chuẩn. Tôi thấy mình sử dụng chúng đủ thường xuyên để làm quen với hoạt động bên trong của chúng

Nếu tôi có thể thay đổi bất cứ điều gì về mô-đun này thì nó sẽ có đối số

# Wrap text to lines of 25 characters
textwrap.wrap[text=sample_text, width=25]

# The result is a list of 25-character long strings [last item may be shorter]
>>> ['On the 24th of February,', '1815, the look-out at', 'Notre-Dame de la Garde', 'signalled the three-', 'master, the _Pharaon_', 'from Smyrna, Trieste, and', 'Naples.  As usual, a', 'pilot put off', 'immediately, and rounding', 'the Château d’If, got on', 'board the vessel between', 'Cape Morgiou and Rion', 'island.  Immediately, and', 'according to custom, the', 'ramparts of Fort Saint-', 'Jean were covered with', 'spectators; it is always', 'an event at Marseilles', 'for a ship to come into', 'port, especially when', 'this ship, like the', '_Pharaon_, has been', 'built, rigged, and laden', 'at the old Phocee docks,', 'and belongs to an owner', 'of the city.']
1 có tên hậu tố, nối thêm hoặc kết thúc để chuyển tải trường hợp sử dụng tốt hơn. Cú pháp
# Wrap text to lines of 25 characters
textwrap.wrap[text=sample_text, width=25]

# The result is a list of 25-character long strings [last item may be shorter]
>>> ['On the 24th of February,', '1815, the look-out at', 'Notre-Dame de la Garde', 'signalled the three-', 'master, the _Pharaon_', 'from Smyrna, Trieste, and', 'Naples.  As usual, a', 'pilot put off', 'immediately, and rounding', 'the Château d’If, got on', 'board the vessel between', 'Cape Morgiou and Rion', 'island.  Immediately, and', 'according to custom, the', 'ramparts of Fort Saint-', 'Jean were covered with', 'spectators; it is always', 'an event at Marseilles', 'for a ship to come into', 'port, especially when', 'this ship, like the', '_Pharaon_, has been', 'built, rigged, and laden', 'at the old Phocee docks,', 'and belongs to an owner', 'of the city.']
1 ngụ ý loại bỏ khi nhập vào—giống như thuộc tính trình giữ chỗ trong trường biểu mẫu HTML. Thật kỳ lạ, phương pháp
# Create a wrap of 25-characters width
textwrap.fill[text=text, width=25]

# Resulting Text
On the 24th of February,
1815, the look-out at
Notre-Dame de la Garde
signalled the three-
master, the _Pharaon_
from Smyrna, Trieste, and
Naples.  As usual, a
pilot put off
immediately, and rounding
the Château d’If, got on
board the vessel between
Cape Morgiou and Rion
island.  Immediately, and
according to custom, the
ramparts of Fort Saint-
Jean were covered with
spectators; it is always
an event at Marseilles
for a ship to come into
port, especially when
this ship, like the
_Pharaon_, has been
built, rigged, and laden
at the old Phocee docks,
and belongs to an owner
of the city.
1 chọn sử dụng cú pháp
# Create a wrap of 25-characters width
textwrap.fill[text=text, width=25]

# Resulting Text
On the 24th of February,
1815, the look-out at
Notre-Dame de la Garde
signalled the three-
master, the _Pharaon_
from Smyrna, Trieste, and
Naples.  As usual, a
pilot put off
immediately, and rounding
the Château d’If, got on
board the vessel between
Cape Morgiou and Rion
island.  Immediately, and
according to custom, the
ramparts of Fort Saint-
Jean were covered with
spectators; it is always
an event at Marseilles
for a ship to come into
port, especially when
this ship, like the
_Pharaon_, has been
built, rigged, and laden
at the old Phocee docks,
and belongs to an owner
of the city.
2

Tôi cũng muốn xem một phương pháp ngắn gọn hơn để hiển thị phiên bản được bao bọc của đối tượng TextWrapper. Ví dụ: để xem văn bản ở dạng được bao bọc, người ta phải chuyển văn bản sang phương thức công khai như vậy.

# Create a wrap of 25-characters width
textwrap.fill[text=text, width=25]

# Resulting Text
On the 24th of February,
1815, the look-out at
Notre-Dame de la Garde
signalled the three-
master, the _Pharaon_
from Smyrna, Trieste, and
Naples.  As usual, a
pilot put off
immediately, and rounding
the Château d’If, got on
board the vessel between
Cape Morgiou and Rion
island.  Immediately, and
according to custom, the
ramparts of Fort Saint-
Jean were covered with
spectators; it is always
an event at Marseilles
for a ship to come into
port, especially when
this ship, like the
_Pharaon_, has been
built, rigged, and laden
at the old Phocee docks,
and belongs to an owner
of the city.
3. Tôi muốn xem thêm nội dung nào đó dọc theo dòng
# Create a wrap of 25-characters width
textwrap.fill[text=text, width=25]

# Resulting Text
On the 24th of February,
1815, the look-out at
Notre-Dame de la Garde
signalled the three-
master, the _Pharaon_
from Smyrna, Trieste, and
Naples.  As usual, a
pilot put off
immediately, and rounding
the Château d’If, got on
board the vessel between
Cape Morgiou and Rion
island.  Immediately, and
according to custom, the
ramparts of Fort Saint-
Jean were covered with
spectators; it is always
an event at Marseilles
for a ship to come into
port, especially when
this ship, like the
_Pharaon_, has been
built, rigged, and laden
at the old Phocee docks,
and belongs to an owner
of the city.
4 vẫn mang lại lợi ích khi truy cập văn bản gốc thông qua thuộc tính
# Create a wrap of 25-characters width
textwrap.fill[text=text, width=25]

# Resulting Text
On the 24th of February,
1815, the look-out at
Notre-Dame de la Garde
signalled the three-
master, the _Pharaon_
from Smyrna, Trieste, and
Naples.  As usual, a
pilot put off
immediately, and rounding
the Château d’If, got on
board the vessel between
Cape Morgiou and Rion
island.  Immediately, and
according to custom, the
ramparts of Fort Saint-
Jean were covered with
spectators; it is always
an event at Marseilles
for a ship to come into
port, especially when
this ship, like the
_Pharaon_, has been
built, rigged, and laden
at the old Phocee docks,
and belongs to an owner
of the city.
5. Tôi đoán tất cả mọi người là một nhà phê bình

Việc sử dụng bọc văn bản là gì?

Sử dụng các điểm bao quanh để điều chỉnh luồng văn bản xung quanh hình dạng .

Ý nghĩa của Textwrap là gì?

Còn được gọi là luồng văn bản, ngắt dòng văn bản là một tính năng trong trình soạn thảo văn bản và trình xử lý văn bản . Nó cho phép văn bản của người dùng được tiếp tục đến dòng tiếp theo khi đến một bên của trang. Trả lại mềm, thuật ngữ xử lý văn bản, bọc văn bản.

Chủ Đề