簡體   English   中英

在文本文件 Python 中搜索大寫單詞的數量

[英]Searching for the amount of capital words in a text file Python

我需要幫助對文本文件進行排序

我嘗試了 for 循環的多種變體。 我還嘗試去除所有空格並單獨計算文件中的字母。 我還嘗試了條帶 function 的多種變體以及不同的 if 語句

for character in file:
    if character.isupper():
        capital += 1
        file.readline().rstrip()
        break

print(capital)

我希望程序能夠讀取文檔中的每個單詞或字母,並返回其中包含的大寫單詞的總數。

如果目標是計算以大寫字母開頭的單詞,那么我會使用 boolean 值是 integer 的子類型的事實:

with open('my_textfile.txt', 'r') as text:
    print(sum(word.istitle() for row in text for word in row))

兩件事情:

  1. 確保您正在迭代字符而不是單詞或句子。 放一些打印語句來檢查。
  2. 刪除 if 塊中的 break 語句。 這將立即退出您的 for 循環,並將導致您只數 1。
for sentence in file:
    for char in sentence:
        if char.isupper():
            capital += 1

print(capital)

假設我們有一個包含以下內容的示例文件doc.txt

這是一個用於識別大寫單詞的測試文件。 我將其創建為示例,因為問題的要求可能會有所不同。 例如,像 SQL 這樣的首字母縮略詞應該算作大寫單詞嗎? 如果否:這應該導致八個大寫單詞。 如果是:這應該導致九個。

如果您想計算大寫(又名標題大小寫)單詞,但不包括首字母縮寫詞之類的全大寫單詞,您可以執行以下操作:

def count_capital_words(filename):                                               
    count = 0                                                                    
    with open(filename, 'r') as fp:                                              
        for line in fp:                                                          
            for word in line.split():                                            
                if word.istitle():                                               
                    print(word)                                                  
                    count += 1                                                   
    return count


print(count_capital_words('doc.txt'))  # 8

如果要計算全大寫單詞,您可以修改 function 以僅檢查單詞的第一個字母。 請注意, filter(None, ...) function 將確保word永遠不是空字符串,避免在這些情況下拋出IndexError

def count_capital_words(filename):                                               
    count = 0                                                                    
    with open(filename, 'r') as fp:                                              
        for line in fp:                                                          
            for word in filter(None, line.split()):                              
                if word[0].isupper():                                            
                    count += 1                                                   
    return count


print(count_capital_words('doc.txt'))  # 9

如果你有更復雜的需求,你可以得到一個這樣的迭代詞:

from itertools import chain                                                      


def get_words(filename):                                                         
    with open(filename, 'r') as fp:                                              
        words = chain.from_iterable(line.split() for line in fp)                 
        yield from words 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM