Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Gói gọn và định dạng văn bản (Text Wrapping and Filling) trong Python

Trong Python, module textwrap dùng để định dạng và gói gọn văn bản thuần túy (plain text). Có một số tùy chọn để định dạng văn bản bằng cách điều chỉnh ngắt dòng trong đoạn văn đầu vào.

Để dùng các module này, cần nhập module textwrap:

import textwrap

Các thuộc tính instance của TextWrapper constructor:

STTThuộc tính & Mô tả
1width – Độ dài tối đa dòng. Mặc định 70
2expand_tabs – Nếu true, tab thay bằng space. Mặc định True
3tabsize – Khi expand_tabs=true, đặt tabsize. Mặc định 8
4replace_whitespace – Thay whitespace bằng space đơn. Mặc định True
5drop_whitespace – Bỏ whitespace đầu/cuối sau wrap. Mặc định True
6initial_indent – Chuỗi tiền tố dòng đầu. Mặc định ''
7subsequent_indent – Chuỗi tiền tố các dòng sau. Mặc định ''
8placeholder – Chuỗi thêm cuối nếu bị cắt. Mặc định '[…]'
9max_lines – Số dòng tối đa sau wrap. None = không giới hạn. Mặc định None
10break_long_words – Ngắt từ dài để vừa width. Mặc định True
11break_on_hyphens – Ngắt tại dấu gạch nối cho từ ghép. Mặc định True

Các phương thức gói gọn văn bản (Text Wrapping Methods)

Có một số phương thức trong module Textwrap:

Phương thức textwrap.wrap(text, width=70, **kwargs) – Gói đoạn văn đầu vào. Dùng độ rộng dòng để wrap. Mặc định 70. Trả về list các dòng. Trong list lưu tất cả dòng đã wrap.

Phương thức textwrap.fill(text, width=70, **kwargs) – Tương tự wrap nhưng không tạo list. Tạo chuỗi (string). Thêm ký tự xuống dòng sau khi vượt width.

Phương thức textwrap.shorten(text, width, **kwargs) – Rút gọn/cắt ngắn chuỗi. Sau cắt, độ dài = width chỉ định. Thêm […] cuối chuỗi.

Mã ví dụ

import textwrap

python_desc = """Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). This tutorial gives enough understanding on Python programming language."""

my_wrap = textwrap.TextWrapper(width=40)
wrap_list = my_wrap.wrap(text=python_desc)
for line in wrap_list:
    print(line)

single_line = """Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language."""
print('\n\n' + my_wrap.fill(text=single_line))

short_text = textwrap.shorten(text=python_desc, width=150)
print('\n\n' + my_wrap.fill(text=short_text))

Kết quả

Python is a general-purpose interpreted,
interactive, object-oriented, and high-level
programming language. It was created by
Guido van Rossum during 1985- 1990. Like
Perl, Python source code is also available
under the GNU General Public License (GPL).
This tutorial gives enough understanding on
Python programming language.

Python is a general-purpose interpreted,
interactive, object-oriented, and high-level
programming language.

Python is a general-purpose interpreted,
interactive, object-oriented, and high-
level programming language. It was created
by Guido van Rossum [...]