我有一列是多行文本。我想匹配一個特定的子字串并將其與所有內容一起提取,直到新行。我面臨的挑戰是同時存在“\n”和“\r”,我還沒有發現如何從我的結果中洗掉“\r”。這是測驗:
\r
empty
\n
\r\n
basic text
\r
WHATIWANT: - this is cool ! = . a1^@#%\r\n
more lines\r\r\r
\n
\r
\n
我想要的結果是:
WHATIWANT: - this is cool ! = . a1^@#%
我嘗試使用:
(WHATIWANT:\s. (.*?)\s )
但是得到這個(不能擺脫 \n 和 \r):
WHATIWANT: - this is cool ! = . a1^@#%\n\r
uj5u.com熱心網友回復:
在多行模式下使用str.extract應該在這里作業:
df["value"] = df["col"].str.extract(r'^(WHATIWANT:.*?)\s*$', flags=re.M)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/417512.html
標籤:
上一篇:計算字串中每個單詞的元音
