簡體   English   中英

如何使用正則表達式刪除制表符和換行符

[英]How to remove tabs and newlines with a regex

在 Python 3.x 中,特殊的重新序列 '\\s' 匹配 Unicode 空白字符,包括 [\\t\\n\\r\\f\\v]。

以下代碼旨在用空格替換制表符和換行符。

import re
text = """Hello my friends.
    How are you doing?
I'm fine."""
output = re.sub('\s', ' ', text)
print(output)

但是,該選項卡仍然存在於輸出中。 為什么?

問題是(可能)您的制表符只是一堆空格。

>>> re.sub(r"\s+", " ", text)
"Hello my friends. How are you doing? I'm fine."

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM