![](/img/trans.png)
[英]How remove all non-alphabet chars excluding white space from string in Python without joining the words
[英]How to remove all the white space from string in Python
我從XML得到一個字符串:
This is the Sample text I need to get
all this but only with single spaces
實際上我使用的是以下reg-ex,它將匹配模式轉換為空白區域,但我需要將它們剝離而不是用空格替換。
應輸出上面的字符串:
這是我需要獲得所有這些但只有單個空格的示例文本
沒有正則表達式的簡單解決方案
s = 'This is the Sample text I need to get all this but only with single spaces'
' '.join(s.split())
#'This is the Sample text I need to get all this but only with single spaces'
為了完整起見,我會在回答中添加ekhumoro的評論:
值得指出的是,split()不僅僅是簡單地拆分空白行:它還會在輸入的開頭和結尾處去掉任何空格(這通常是想要的)。 如果輸入是unicode(可能與XML相關),它還可以正確處理不間斷空格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.