Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中从文本文件中挑选特定的单词?_Python - Fatal编程技术网

如何在python中从文本文件中挑选特定的单词?

如何在python中从文本文件中挑选特定的单词?,python,Python,这是我的文本文件:“myFile.txt” 现在我想把单词“far”取出来,它出现了多少次,打印出来的是相同的。如果有人能帮我解决这个问题,请解释,提前谢谢。只需使用Python中内置的.count()方法即可 my_string = "Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated

这是我的文本文件:“myFile.txt”


现在我想把单词“far”取出来,它出现了多少次,打印出来的是相同的。如果有人能帮我解决这个问题,请解释,提前谢谢。

只需使用Python中内置的
.count()
方法即可

my_string = "Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then"

print(my_string.count("far"))
将给出一个输出
3


如果需要使其不区分大小写,可以使用
.lower()

将my_字符串转换为小写,只需使用Python中内置的
.count()
方法即可

my_string = "Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then"

print(my_string.count("far"))
将给出一个输出
3


如果需要使其不区分大小写,可以使用
.lower()

将my_字符串转换为小写。这是否回答了您的问题?这回答了你的问题吗?我想把单词打印出来我也想打印出来