我最近在python3.5中编写了这个脚本来搜索文本文件中给定的字符串,我似乎不知道如何让脚本在“log”一词出现在行中之后删除其余的单词。你知道吗
file1 = input ('What is the name of the file? ')
search_string = input ('What are you looking for? ')
with open(file1) as cooldude:
for line in cooldude:
line = line.rstrip()
if search_string in line:
print(line)
例如: “我想保留这些东西。“我不想要这些东西。” 我想删除之后的一切,包括“日志”一词。谢谢!你知道吗
如果您只想删除行中模式} 输出的第一部分或^{} 的第0个索引:
'log'
之后的文本部分,则可以使用^{对于一个微小的变化,可以使用^{} 和
maxsplit=1
来移除最后一个'log'
之后的部分:相关问题 更多 >
编程相关推荐