我有一个只有一个大字符串的文件。在这个字符串中,有些句子以3个数字结尾,如下所示:
sees mouse . 1980 1 1 sheep erythrocytes mouse 1980 6 5 seen mouse 1980 8 8
我想将其更改为文件/输出如下所示:
sees mouse . 1980 1 1
sheep erythrocytes mouse 1980 6 5
seen mouse 1980 8 8
下面是我用来解决这个问题的代码:
with open('ngram_test') as f:
for line in f:
#print(line)
for word in line.split():
print(word)
但是,它只打印字符串中的每个单词和换行符。任何帮助将不胜感激!在
使用Regex,可以在每个模式出现后添加新行(
\n
):输出:
^{pr2}$您需要使用
regexp
并查找所需字符串的索引,然后将其删除。在假设输入受到问题中提供的字符串的限制,这将起作用。如果不是,则需要扩展模式。在
一个简单的正则表达式就可以了
相关问题 更多 >
编程相关推荐