2021年12月9日 星期四

Python 學習筆記 : 讓文章每 80 行換列的方法

長字串文本在使用 print() 列印時會連續輸出直到遇到 '\n' 字元才會換列輸出, 在文字編輯器中開啟這種長字串文本時也是如此, 長度超出編輯視窗寬度時是以水平捲軸來捲動 (但輸出到印表機時遇到列印邊界會自動折返換列列印), 例如理性與感性的第一章第一段 :

The family of Dashwood had long been settled in Sussex. Their estate was large, and their residence was at Norland Park, in the centre of their property, where, for many generations, they had lived in so respectable a manner as to engage the general good opinion of their surrounding acquaintance. The late owner of this estate was a single man, who lived to a very advanced age, and who for many years of his life, had a constant companion and housekeeper in his sister. But her death, which happened ten years before his own, produced a great alteration in his home; for to supply her loss, he invited and received into his house the family of his nephew Mr. Henry Dashwood, the legal inheritor of the Norland estate, and the person to whom he intended to bequeath it. In the society of his nephew and niece, and their children, the old Gentleman’s days were comfortably spent. His attachment to them all increased. The constant attention of Mr. and Mrs. Henry Dashwood to his wishes, which proceeded not merely from interest, but from goodness of heart, gave him every degree of solid comfort which his age could receive; and the cheerfulness of the children added a relish to his existence.

參考 : 


將它貼到記事本是長長的兩列, 需移動底下的水平捲軸才能看到全文 : 




有沒有辦法讓這個文本不要超過 80 行? 換句話說就是讓它在遇到右邊的第 80 行邊界時就自動跳行 (即加上 '\n') ? 這可以利用字串的 split() 方法以及串列操作來達成, 首先將此文本存成 Python 的長字串 : 

>>> text='''
The family of Dashwood had long been settled in Sussex. Their estate was large, and their residence was at Norland Park, in the centre of their property, where, for many generations, they had lived in so respectable a manner as to engage the general good opinion of their surrounding acquaintance. The late owner of this estate was a single man, who lived to a very advanced age, and who for many years of his life, had a constant companion and housekeeper in his sister. But her death, which happened ten years before his own, produced a great alteration in his home; for to supply her loss, he invited and received into his house the family of his nephew Mr. Henry Dashwood, the legal inheritor of the Norland estate, and the person to whom he intended to bequeath it. In the society of his nephew and niece, and their children, the old Gentleman’s days were comfortably spent. His attachment to them all increased. The constant attention of Mr. and Mrs. Henry Dashwood to his wishes, which proceeded not merely from interest, but from goodness of heart, gave him every degree of solid comfort which his age could receive; and the cheerfulness of the children added a relish to his existence.'''

然後呼叫字串物件的 split() 方法, 它預設會以空格為分界字元將字串拆成串列傳回 : 

>>> words=text.split()      # 將文本以空格為界拆成串列
>>> len(words)    
209

可見第一段內容包含 209 個字. 

然後準備一個空串列 lines 來存放長度接近 80 行的各列句子, 還需要一個用來存放處理中的單列字串 line, 預設為空字串 : 
  
>>> lines=[]          # 儲存長度接近 80 行的各列句子
>>> line=''            # 列變數 : 暫存尚未接近 80 行的處理中字串
>>> width=80       # 切齊的行數 (文件寬度)

接下來就可以用迴圈走訪 words 串列, 判斷目前這列長度加上現在走訪的這個字長度是否大於 79 (留 1 個字元邊限), 是的話就把目前這列放進 lines 串列中並清空列變數 line (即關閉一列), 否則就把目前這個字前面插一個空格接在列變數 line 後面 : 

>>> for word in words:                             # 走訪文本中的字元素
    if len(line) + len(word) + 1 > width:     # 是否超出寬度? 是就關閉這列
        lines.append(line)                               # 將這列存入串列 lines 
        line=''                                                   # 重啟新列, 重設列變數 line
    else:                                                          # 還沒有到達關閉列條件
        line=line + ' ' + word                          # 將目前的字冠一個空格接在列變數後面
    if line != '':                                               # 最後一列不為空
        lines.append(line)                                # 加入最後一列

經過上面程序處理後, 串列 lines 裡面就存放了接近 80 行的各列句子, 然後將 lines 串列中的各列用換列字元 '\n' 串接起來即可得到我們所要的結果 : 

>>> result='\n'.join(lines)    
>>> print(result)   
 The family of Dashwood had long been settled in Sussex. Their estate was large,
 their residence was at Norland Park, in the centre of their property, where,
 many generations, they had lived in so respectable a manner as to engage the
 good opinion of their surrounding acquaintance. The late owner of this estate
 a single man, who lived to a very advanced age, and who for many years of his
 had a constant companion and housekeeper in his sister. But her death, which
 ten years before his own, produced a great alteration in his home; for to
 her loss, he invited and received into his house the family of his nephew Mr.
 Dashwood, the legal inheritor of the Norland estate, and the person to whom he
 to bequeath it. In the society of his nephew and niece, and their children, the
 Gentleman’s days were comfortably spent. His attachment to them all increased.
 constant attention of Mr. and Mrs. Henry Dashwood to his wishes, which
 not merely from interest, but from goodness of heart, gave him every degree of
 comfort which his age could receive; and the cheerfulness of the children added

可見新的文本都在接近 80 行時換列了. 

可以用 with open 指令將結果寫入檔案 result.txt 中 :

>>> with open('result.txt', 'w') as f:      # 將結果寫入檔案
    f.write(result)   
    
1126

開啟 result.txt 可知文本已經被改為接近 80 行時換列 : 




完整的程式如下 : 

text='''
The family of Dashwood had long been settled in Sussex. Their estate was large, and their residence was at Norland Park, in the centre of their property, where, for many generations, they had lived in so respectable a manner as to engage the general good opinion of their surrounding acquaintance. The late owner of this estate was a single man, who lived to a very advanced age, and who for many years of his life, had a constant companion and housekeeper in his sister. But her death, which happened ten years before his own, produced a great alteration in his home; for to supply her loss, he invited and received into his house the family of his nephew Mr. Henry Dashwood, the legal inheritor of the Norland estate, and the person to whom he intended to bequeath it. In the society of his nephew and niece, and their children, the old Gentleman’s days were comfortably spent. His attachment to them all increased. The constant attention of Mr. and Mrs. Henry Dashwood to his wishes, which proceeded not merely from interest, but from goodness of heart, gave him every degree of solid comfort which his age could receive; and the cheerfulness of the children added a relish to his existence.'''
words=text.split()
lines=[]
line=''
width=80
for word in words:                                         
    if  len(line) + len(word) + 1 > width:  
        lines.append(line)         
        line=''    
    else:                                       
        line=line + ' ' + word
if line != '':
    lines.append(line)
result='\n'.join(lines)
print(result)

沒有留言 :