从文本文件的每一行中删除一段可变文本

Delete a variable piece of text from every line in textfile

我有一个包含以下行的 .txt 文件:

username:password:*email*:email_address, where *email* is a fixed word throughout the file.

我想删除第一个:之后和第二个:之前的所有内容,换句话说,删除密码,只有username:*email*:email_address.

谁能帮帮我?谢谢

您可以像这样编写 Python 脚本:

with open('file.txt') as f:
    for line in f.readlines():
        fields = line.split(':')
        del fields[1]
        print ':'.join(fields),

并将输出重定向到新文件。