首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的python程序只分割我文本文件的最后一段,有人知道为什么会这样吗?

我的python程序只分割我文本文件的最后一段,有人知道为什么会这样吗?
EN

Stack Overflow用户
提问于 2014-03-23 06:40:45
回答 2查看 98关注 0票数 0

好的,所以我试着解决这个关于在文本文件中找到单词的练习,它应该打印出多少单词。所以我试着先把单词分开,这样就更容易找到特定的单词了。然而,当我试图把两段分开时,它只分裂了第二段。

文本文件是这样写的:

“比尔和泰德的卓越冒险是一部1989年美国科幻喜剧巴迪电影,也是比尔·特德系列电影中的第一部电影,两名懒汉穿越时间聚集了一大群历史人物,为他们的高中历史演讲。”

这部电影由克里斯·马西森和埃德·所罗门执导,斯蒂芬·赫雷克执导。基努·里维斯饰演泰德·“西奥多”·洛根,亚历克斯·温特饰演比尔·S·普雷斯顿,埃斯奎尔和乔治·卡林饰演鲁弗斯。比尔和泰德的优秀冒险获得了好评,大多是积极的发布和商业上的成功。它现在被认为是一个邪教经典。两年后上映了一部续集“比尔和泰德的假旅程”。一部未命名的第三部电影正在制作中。“

这样做的目的是找出段落中有多少个“比尔”。

代码语言:javascript
复制
f = open("text.txt", "r")
listBill = []
for line in f:
    print(line)

splitParagraph = line.split()
print(splitParagraph)
for eachBill in splitParagraph:
    if eachBill == "Bill":
        listBill.append("Bill")
print(listBill)

但我得到的却是:

代码语言:javascript
复制
Bill & Ted's Excellent Adventure is a 1989 American science fiction comedy buddy film 
and the first film in the Bill & Ted franchise in which two slackers travel through time
to assemble a menagerie of historical figures for their high school history resentation.

The film was written by Chris Matheson and Ed Solomon and directed by Stephen Herek. It
stars Keanu Reeves as Ted "Theodore" Logan, Alex Winter as Bill S. Preston, Esquire, and
George Carlin as Rufus. Bill & Ted's Excellent Adventure received reviews which were 
mostly positive upon release and was commercially successful. It is now considered a 
cult classic. A sequel, Bill & Ted's Bogus Journey, was released two years later. An 
untitled third film is in development

['The', 'film', 'was', 'written', 'by', 'Chris', 'Matheson', 'and', 'Ed', 'Solomon',  
'and', 'directed', 'by', 'Stephen', 'Herek.', 'It', 'stars', 'Keanu', 'Reeves', 'as', 
'Ted', '"Theodore"', 'Logan,', 'Alex', 'Winter', 'as', 'Bill', 'S.', 'Preston,',  
'Esquire,',    'and', 'George', 'Carlin', 'as', 'Rufus.', 'Bill', '&', "Ted's",     
'Excellent',     'Adventure', 'received', 'reviews', 'which', 'were', 'mostly',      
 positive', 'upon', 'release', 'and', 'was', 'commercially', 'successful.', 'It', 'is',
 now', 'considered', 'a', 'cult', 'classic.', 'A', 'sequel,', 'Bill', '&', "Ted's", 
 Bogus', 'Journey,', 'was', 'released', 'two', 'years', 'later.', 'An', 'untitled', 
 'third', 'film', 'is', 'in', 'development']

['Bill', 'Bill', 'Bill']

正如你所看到的,它只找到了3“比尔”,因为它只读了第二段。我试着寻找和我有同样问题的人,但似乎只有我一个人。请注意,程序可以打印前两段而不分裂。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-23 06:43:57

你的压痕是错误的,应该是:

代码语言:javascript
复制
f = open("text.txt", "r")
listBill = []
for line in f:
    print(line)

    splitParagraph = line.split()
    print(splitParagraph)
    for eachBill in splitParagraph:
        if eachBill == "Bill":
            listBill.append("Bill")

print(listBill)
票数 1
EN

Stack Overflow用户

发布于 2014-03-23 07:44:16

在字符串中查找单词有一种更容易的方法。只需使用字符串的计数法

代码语言:javascript
复制
s = """Bill & Ted's Excellent Adventure is a 1989 American science fiction comedy buddy film and the first film in the Bill & Ted franchise in which two slackers travel through time to assemble a menagerie of historical figures for their high school history presentation.

The film was written by Chris Matheson and Ed Solomon and directed by Stephen Herek. It stars Keanu Reeves as Ted "Theodore" Logan, Alex Winter as Bill S. Preston, Esquire, and George Carlin as Rufus. Bill & Ted's Excellent Adventure received reviews which were mostly positive upon release and was commercially successful. It is now considered a cult classic. A sequel, Bill & Ted's Bogus Journey, was released two years later. An untitled third film is in development"""

print(s.count("Bill"))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22587859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档