我怎樣才能計算一個txt文件的第一行有多少「a」。第二次等多少次?以及如何計算整個文件中一個字母的百分比?我知道如何在一個字符串中做到這一點,但沒有把它放在一個文件中,逐行... sry im nooob ,,,,計算一個字母出現在txt文件的一行中的次數。在python 2.3
-7
A
回答
0
如果我正確理解你的問題,像這樣的工作:
read_lines = open("as.txt", "r") #this opens and reads the entire file
line_number = 1 #setting a var to the line number we're at
for word in read_lines: #starts looping for each word in the line
number_of_as = 0 #we are going to keep track of all the a's here for each line
for letter in word: #for each letter in each word in the line
if letter == "a" or letter == "A": #if the letter is a a or A
number_of_as += 1 #add 1 to how many a's are in this line
print("Line " + str(line_number) + " has " + str(number_of_as) + " a's")
line_number += 1 #now we move to the next line so we add 1 to our tracker
這將讀取整個文件,到每個字的文件中,並檢查每個字母。如果這封信是a
,那麼計數器number_of_as
增加1。在程序結束時,將顯示總數a
。
但是,這是用python 3編寫的。
現在的代碼顯示了每行a
的數量。
+0
即時通訊不知道它是否會在Python 2中工作,但我會嘗試。非常感謝 ! –
+0
請讓我知道它是否工作。 – GreenSaber
+0
但如何將它逐行排列?我的意思是如何得到第一行有多少「a」和第二行有多少?再次感謝你!欣賞它。 –
0
相關問題
- 1. 計算一個字母在python中出現在文本文件中的次數
- 2. 計算一個字符串出現在文件中的次數
- 3. 使用python計算一個字母在某個位置出現的次數
- 4. Java計算一個字符出現在一個文件中的次數
- 5. 計算單詞在txt文件中出現的次數Java
- 6. 計算.txt輸入文件中的字符串出現次數
- 7. 計算兩個字母一起出現的次數
- 8. 計算單個字母出現在單詞中的次數
- 9. 計算文件中字母的出現次數
- 10. 如何計算另一個.txt文件中項目列表的出現次數?
- 11. 如何算一個字有多少次出現在一個txt文件
- 12. 如何統計一個csv文件的每一行出現的次數,在python
- 13. 如何計算字母出現在字符串中的次數?
- 14. 計算txt文件中特定文本的出現次數
- 15. 計算字母表中每個字母出現在ArrayList的一系列字符串中的次數
- 16. 計算字符串中某個字母的出現次數
- 17. 計算字符串中每個字母的出現次數
- 18. 如何計算一個單詞在一個句子中出現的次數? (Python)
- 19. 一個算術計算每個數字在一個txt文件中由perl
- 20. 計算一個值出現在這個數組中的次數?
- 21. 如何計算單詞在一列中出現的次數,python
- 22. 計算新的一行/行的出現次數出現
- 23. 計算一個字符串出現在一個序列中的次數
- 24. 如何計算使用python顯示一個字母的次數?
- 25. Python:計算文件中某個單詞出現的次數
- 26. 計算一個單詞在php數組中出現的次數
- 27. 計算一個值出現在多維數組中的次數
- 28. 我的程序需要計算一個單詞中字母的出現次數,但它似乎只計算字母'a'的出現次數
- 29. 計算字符串出現在文件中的次數
- 30. 計數字母表一次出現在列表中
Python 2.3?真?無論如何:你有什麼嘗試? – vaultah
2k17中的Python-2.3? – shash678
[計算字符串中字符出現次數]可能的重複(https://stackoverflow.com/questions/1155617/count-occurrence-of-a-character-in-a-string) – ptyyy