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

2024-09-29 19:20:43 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,我正在解决这个关于在文本文件中查找单词的练习,它应该打印出单词的数量。所以我试着先把单词分开,这样就更容易找到特定的单词。然而,当我尝试分割两段时,它只分割第二段。你知道吗

文本文件是这样写的:

《比尔与泰德的精彩冒险》是一部1989年的美国科幻喜剧《老友记》,也是比尔与泰德系列电影中的第一部,两个懒汉穿越时空,汇集了一批历史人物,为他们的高中历史展示作准备。

这部电影由克里斯·马西森和埃德·所罗门编剧,斯蒂芬·赫雷克执导。影片由基努·里维斯饰演泰德·西奥多·洛根,亚历克斯·温特饰演比尔·S·普雷斯顿,艾斯奎尔和乔治·卡林饰演鲁弗斯。比尔和泰德的出色冒险经历在发行时获得了好评,并且在商业上取得了成功。它现在被认为是一个邪教经典。续集《比尔和泰德的虚假之旅》在两年后上映。一部未命名的第三部电影正在拍摄中“

这样做的目的是找出段落中有多少个“Bill”。你知道吗

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)

但我得到的却是:

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个“Bill”,因为它只读了第二段。我试着寻找和我有同样问题的人,但似乎只有我一个人。请注意,程序可以打印前两段而不拆分。你知道吗


Tags: andinforby电影isasit
2条回答

在字符串中查找单词有一种更简单的方法。只需使用字符串的count method

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"))

您的缩进错误,应该是:

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)

相关问题 更多 >

    热门问题