Python 如何删除文本的前10个单词?
我有一个txt文件,里面有一首Gibrán Halil Gibrán的诗,我必须从中删除前10个单词,可能还有一张地图,然后删除,但我不能把它放在一起。我用pop()试过这个。但我不知道该怎么做Python 如何删除文本的前10个单词?,python,python-3.x,word,remove,txt,Python,Python 3.x,Word,Remove,Txt,我有一个txt文件,里面有一首Gibrán Halil Gibrán的诗,我必须从中删除前10个单词,可能还有一张地图,然后删除,但我不能把它放在一起。我用pop()试过这个。但我不知道该怎么做 for word in prophet: if word is [0]: prophet.pop[0] if word is [1]: prophet.pop[1] print(prophet) 请在此处找到一个示例: >>
for word in prophet:
if word is [0]:
prophet.pop[0]
if word is [1]:
prophet.pop[1]
print(prophet)
请在此处找到一个示例:
>>> text = "I have a txt file with a poem by Gibrán Halil Gibrán, from where I have to delete the first 10 words, probably with a map and delete, but I can't put it together. I tried this with pop (). But i don't have idea about how can i doit."
>>> text = ' '.join(text.split()[10:])
>>> text
"Halil Gibrán, from where I have to delete the first 10 words, probably with a map and delete, but I can't put it together. I tried this with pop (). But i don't have idea about how can i doit."
调用以下函数以删除前10个字:
def删除单词(文本):
words=text.split()
return“.join(单词[10:])
例如,整个脚本可能看起来像:
打开(“file.txt”)作为f:
text=f.read()
text=删除单词(text)
打开(“new_file.txt”、“w”)作为f:
f、 书写(文本)
您可以简单地使用拆分方法:
s=“a bc d ef gh i jk l m p q r s”
ans=s.split(“,10)[1]
打印(ans)#给出“q r s”
什么是先知
?是列表还是字符串?另外,您认为单词[0]
是什么意思?