Pythonで改行(\n)が含まれていた場合に、\nより前の文字列だけを抽出したい場合の対応方法についてメモします。
reで正規表現を使って抽出
import re
sample_text = "あああ\nいいい\nううう"
result = re.search('(.*)\\n', sample_text).group(1)
print(result) # あああ
exit()
reで正規表現を使います。
今回は以上となります。
Linux
Docker
Code
Program学習
PlantUML
Linux
Mac
Docker
CI
Powershell
Code
Powershell
Python
IaC
Code
Powershell
Powershell
Code
PythonPythonで改行(\n)が含まれていた場合に、\nより前の文字列だけを抽出したい場合の対応方法についてメモします。
import re
sample_text = "あああ\nいいい\nううう"
result = re.search('(.*)\\n', sample_text).group(1)
print(result) # あああ
exit()
reで正規表現を使います。
今回は以上となります。
コメント