shhehwushh 2021-06-05 20:53 采纳率: 63.2%
浏览 27

来位大神救救孩子吧,一道文件类题

 

  • 写回答

1条回答 默认 最新

  • keyanhunzi111 2023-02-25 22:40
    关注
    
    import re
    
    # 读取输入文件
    with open("trans.in", "r") as fin:
        input_str = fin.readline().strip()
    
    # 正则表达式匹配连续的数字
    pattern = re.compile(r"\d+")
    numbers = pattern.findall(input_str)
    
    # 写入输出文件
    with open("trans.out", "w") as fout:
        fout.write(str(len(numbers)) + "\n")
        for num in numbers:
            fout.write(num + " ")
        fout.write("\n")
    

    希望可以帮助到你

    评论

报告相同问题?