• input输入多行文本:删除“首先 其次 此外 总的来说”


    原标题

    input输入多行文本:删除“首先 其次 此外 总的来说”
    
    • 1
    
    input允许多行输入
    233.3表示停止输入
    
    input输入多行文本
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    文本

    (空行) (空行) (空行) 正文 (空行) (空行) (空行) 正文 (空行) (空行) (空行) 正文 (空行) (空行) (空行) (空行) 正文 (空行) 正文 (空行) 正文 (空行) (空行) (空行) 正文 (空行) (空行) (空行) 正文 (空行) (空行) 正文 (空行) 正文 (空行) 正文 (空行) 正文 (空行) (空行)
    
    把
    >2个空行
    正文
    >1个空行
    
    查找替换:'# 正文'
    
    
    在
    正文
    (空行)
    正文
    (空行)
    正文
    (空行)
    正文
    (空行)
    里每个段落的第1个字至第1个标点符号那里,通常是,逗号,看看有
    “首先 其次 此外 总的来说”
    随机删除前3个或者后3个。
    比如删除“首先 其次 此外“或者保留“首先 总的来说”
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    233.3
    Traceback (most recent call last):
      File "/storage/emulated/0/qpython/删除行 1.3.2.1.查找替换“首先其次此外总的来说”1.1. 1..2.py", line 51, in 
        new_text = process_text(text)
                   ^^^^^^^^^^^^^^^^^^
      File "/storage/emulated/0/qpython/删除行 1.3.2.1.查找替换“首先其次此外总的来说”1.1. 1..2.py", line 32, in process_text
        idx = random.randint(0, len(keywords)  - 1)
              ^^^^^^
    NameError: name 'random' is not defined
    1|:/ $
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    由于漏掉了导入random模块,在程序开头添加以下代码即可:

    import random
    
    • 1

    然后再次运行程序就可以了。

    在这里插入图片描述

    你的输入格式比较特殊,需要按照一定格式进行处理。以下是代码实现:

    def process_text(text):
        # 先按照多个空行分割文本
        paragraphs = text.split('\n\n\n\n')
        new_text = ''
        for para in paragraphs:
            # 去除段落开头结尾的空白符
            para = para.strip()
            if not para:
                continue
            # 替换掉指定字符
            para = para.replace('>2个空行\n', '')
            para = para.replace('>1个空行\n', '# 正文\n')
            # 将每个段落按照标点符号分割成句子
            sentences = para.split('。')
            new_para = ''
            for sent in sentences:
                # 如果句子为空或者只包含空白符则跳过
                if not sent.strip():
                    continue
                # 找到第一个标点符号的位置
                punc_index = len(sent)
                for punc in [',', '。', ',', '.', '!', '?']:
                    index = sent.find(punc)
                    if index != -1 and index < punc_index:
                        punc_index = index
                # 我们只考虑第一个标点符号之前的内容
                first_part = sent[:punc_index]
                # 随机删除前3个或者后3个
                keywords = ['首先', '其次', '此外', '总的来说']
                for i in range(3):
                    if keywords:
                        idx = random.randint(0, len(keywords) - 1)
                        keyword = keywords.pop(idx)
                        if keyword in first_part:
                            first_part = first_part.replace(keyword, '')
                # 将处理后的内容拼接起来
                new_sent = first_part + sent[punc_index]
                new_para += new_sent
            # 将处理后的段落拼接到最终的文本中
            new_text += new_para + '\n\n'
        return new_text
    
    # 读取多行输入,直到输入为"233.3"
    text = ''
    while True:
        line = input()
        if line == '233.3':
            break
        text += line + '\n'
    
    new_text = process_text(text)
    print(new_text)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    你可以将上面的代码复制到Python环境中运行,输入你的多行文本,然后等待程序输出即可。

  • 相关阅读:
    写在大二结束
    5G与中国的海
    如何获取用户请求的真实ip,并返回访问者的ip地理位置?node,vue
    代码随想录训练营第42天|01背包问题、LeetCode 416. 分割等和子集
    Linux权限总结
    【软件工程之美 - 专栏笔记】“一问一答”第3期 | 18个软件开发常见问题解决策略
    【NowCoder】左程云-设计getMin功能的栈
    防火墙基础技术
    MybatisPlus
    剑指 Offer 09. 用两个栈实现队列
  • 原文地址:https://blog.csdn.net/weixin_73675558/article/details/133604809