- Python - 文字處理
- Python - 文字處理簡介
- Python - 文字處理環境
- Python - 字串不變性
- Python - 排序行
- Python - 段落重新格式化
- Python - 統計段落中的詞元
- Python - 二進位制ASCII轉換
- Python - 字串作為檔案
- Python - 反向檔案讀取
- Python - 過濾重複單詞
- Python - 從文字中提取電子郵件
- Python - 從文字中提取URL
- Python - 美化列印
- Python - 文字處理狀態機
- Python - 首字母大寫和翻譯
- Python - 詞元化
- Python - 刪除停用詞
- Python - 同義詞和反義詞
- Python - 文字翻譯
- Python - 單詞替換
- Python - 拼寫檢查
- Python - WordNet 介面
- Python - 語料庫訪問
- Python - 詞性標註
- Python - 組塊和組塊間隙
- Python - 組塊分類
- Python - 文字分類
- Python - 二元語法
- Python - 處理PDF
- Python - 處理Word文件
- Python - 讀取RSS feed
- Python - 情感分析
- Python - 搜尋和匹配
- Python - 文字處理
- Python - 文字換行
- Python - 頻率分佈
- Python - 文字摘要
- Python - 詞幹提取演算法
- Python - 受約束的搜尋
Python - 段落重新格式化
當處理大量文字並將其整理成可呈現的格式時,需要對段落進行格式化。我們可能只想以特定的寬度列印每一行,或者在列印詩歌時嘗試為每一行增加縮排。在本章中,我們使用名為textwrap3的模組來根據需要格式化段落。
首先,我們需要安裝所需的包,如下所示
pip install textwrap3
固定寬度換行
在這個例子中,我們為段落的每一行指定了30個字元的寬度。透過為width引數指定一個值來使用wrap函式。
from textwrap3 import wrap
text = 'In late summer 1945, guests are gathered for the wedding reception of Don Vito Corleones daughter Connie (Talia Shire) and Carlo Rizzi (Gianni Russo). Vito (Marlon Brando), the head of the Corleone Mafia family, is known to friends and associates as Godfather. He and Tom Hagen (Robert Duvall), the Corleone family lawyer, are hearing requests for favors because, according to Italian tradition, no Sicilian can refuse a request on his daughters wedding day.'
x = wrap(text, 30)
for i in range(len(x)):
print(x[i])
執行上述程式後,我們將得到以下輸出:
In late summer 1945, guests are gathered for the wedding reception of Don Vito Corleones daughter Connie (Talia Shire) and Carlo Rizzi (Gianni Russo). Vito (Marlon Brando), the head of the Corleone Mafia family, is known to friends and associates as Godfather. He and Tom Hagen (Robert Duvall), the Corleone family lawyer, are hearing requests for favors because, according to Italian tradition, no Sicilian can refuse a request on his daughters wedding day.
可變縮排
在這個例子中,我們增加了要列印的詩歌每一行的縮排。
import textwrap3
FileName = ("path\poem.txt")
print("**Before Formatting**")
print(" ")
data=file(FileName).readlines()
for i in range(len(data)):
print data[i]
print(" ")
print("**After Formatting**")
print(" ")
data=file(FileName).readlines()
for i in range(len(data)):
dedented_text = textwrap3.dedent(data[i]).strip()
print dedented_text
執行上述程式後,我們將得到以下輸出:
**Before Formatting** Summer is here. Sky is bright. Birds are gone. Nests are empty. Where is Rain? **After Formatting** Summer is here. Sky is bright. Birds are gone. Nests are empty. Where is Rain?
廣告