You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何格式化正则提取的句子列表?去除\n并每行显示单句

规整格式化提取的句子列表

你需要将提取到的包含关键词的句子做两个处理:一是去除句子中的\n字符,二是让每个句子单独占一行输出,当前代码直接打印列表会输出带括号和引号的格式,不符合要求。

可以通过以下两种方式修改代码实现需求:

方式一:逐个打印清理后的句子

words = ['billion']
# 提取句子时同步去除内部的\n字符
sentences = [sentence.replace('\n', '') for sentence in text_1 if any(
    w.lower() in sentence.lower() for w in words)]

# 遍历列表,每个句子单独打印一行
for sentence in sentences:
    print(sentence)

方式二:用换行符拼接后一次性打印

words = ['billion']
sentences = [sentence.replace('\n', '') for sentence in text_1 if any(
    w.lower() in sentence.lower() for w in words)]

# 用换行符连接所有句子,一次性输出实现每行一句
print('\n'.join(sentences))

修改后,句子内部的\n会被彻底移除,且每个句子单独占据一行输出,格式更规整。

内容的提问来源于stack exchange,提问作者nji1997

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 20:51:19