Nội dung
- Giới thiệu
- Cú pháp - đếm []
- Ví dụ 1: Đếm số lần một từ xảy ra trong tệp văn bản đã cho
- Bản tóm tắt
Để đếm số lần xuất hiện của một từ cụ thể trong tệp văn bản, hãy đọc nội dung của tệp văn bản vào một chuỗi và sử dụng hàm chuỗi.count [] với từ được truyền làm đối số cho hàm đếm [].
Cú pháp - đếm []
Ví dụ 1: Đếm số lần một từ xảy ra trong tệp văn bản đã cho
n = String.count[word]
Bản tóm tắt
Ví dụ 1: Đếm số lần một từ xảy ra trong tệp văn bản đã cho
Bản tóm tắt
Để đếm số lần xuất hiện của một từ cụ thể trong tệp văn bản, hãy đọc nội dung của tệp văn bản vào một chuỗi và sử dụng hàm chuỗi.count [] với từ được truyền làm đối số cho hàm đếm [].
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
Sau đây là cú pháp của hàm đếm [].
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
trong đó
0 là chuỗi và Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1 trả về số lần xuất hiện của Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
0 trong chuỗi này.Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
Number of occurrences of the word : 2
Bản tóm tắt
Để đếm số lần xuất hiện của một từ cụ thể trong tệp văn bản, hãy đọc nội dung của tệp văn bản vào một chuỗi và sử dụng hàm chuỗi.count [] với từ được truyền làm đối số cho hàm đếm [].
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry8
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
5 Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
00#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
7 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry7
Tệp mẫu.txt
Đầu tiên, chúng tôi tạo một tệp văn bản trong đó chúng tôi muốn đếm các từ trong Python. Đặt tệp này được mẫu.txt với các nội dung sausample.txt with the following contents
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry
Ví dụ 1: đếm các lần xuất hiện của mỗi từ trong một tệp văn bản đã cho Count occurrences of each word in a given text file
Ở đây, chúng tôi sử dụng một vòng python để đọc từng dòng và từ dòng đó, chúng tôi đang chuyển đổi từng dòng thành thấp hơn cho số lượng duy nhất và sau đó chia từng từ để đếm số của nó.
Python3
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
5Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
7Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
8Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
9#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
0#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
1Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 #get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
3#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
4#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
5 #get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
6#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
7 #get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
8#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
9#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
6Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 Number of occurrences of the word : 2
2#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
9#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
6Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 Number of occurrences of the word : 2
6#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
9Number of occurrences of the word : 2
8Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry0
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
0#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
9#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
5 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry5
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
7 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry7
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry8
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry9
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry5
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
7 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 12
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 13
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 14
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 14
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 17
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 18
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry8
Occurrences of the word apple: 20
Occurrences of the word apple: 21
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 13
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 14
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 18
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
5 Occurrences of the word apple: 27
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
7 Occurrences of the word apple: 29
Total Number of Words: 150
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
9Total Number of Words: 152
Total Number of Words: 153
Total Number of Words: 154
Total Number of Words: 155
Output:
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 1
Ví dụ 2: đếm các từ của các từ cụ thể trong một tệp văn bản đã cho 2: Count occurrences of specific words in a given text file
Trong ví dụ này, chúng tôi sẽ đếm số lượng táo có mặt trong tệp văn bản.
Python3
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry5
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 Total Number of Words: 158
Total Number of Words: 159
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.1
Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.2
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
5Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.5
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
8Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.7
Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.8
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
9#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
5 #get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
6#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
7 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 13
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry8
Number of occurrences of the word : 2
8Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 17
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry8
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
5 Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
00#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
7 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry7
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 13
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry9
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
05Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
08Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
09Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
10Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
10mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 17
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 18
Total Number of Words: 152
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
17Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
18Total Number of Words: 154
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
20Output:
Occurrences of the word apple: 2
Ví dụ 3: Đếm tổng số xuất hiện của các từ trong một tệp văn bản đã cho 3: Count total occurrences of words in a given text file
Trong ví dụ này, chúng tôi sẽ đếm tổng số từ có trong một tệp văn bản.
Python3
Total Number of Words: 159
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.1
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
9#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
5 #get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
6#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
7 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 13
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry8
Number of occurrences of the word : 2
8Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 17
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry8
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
5 Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
00#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
7 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry7
Total Number of Words: 152
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
17Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
18Total Number of Words: 154
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
20Ví dụ 3: Đếm tổng số xuất hiện của các từ trong một tệp văn bản đã cho
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
54Output:
Total Number of Words: 15
Trong ví dụ này, chúng tôi sẽ đếm tổng số từ có trong một tệp văn bản.
Sample.txt:
Mango! banana apple pear. Banana, grapes strawberry. Apple- pear mango banana. Kiwi "apple" mango strawberry.
Code:
Python3
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
24Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
5Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
7Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
8Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
9#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
0Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
5Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
7Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
8Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
9#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
0#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
1Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 #get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
3#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
4#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
5 #get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
6#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
7 #get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
8#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
9#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
6Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 Number of occurrences of the word : 2
2#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
9#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
6Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 Number of occurrences of the word : 2
6#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
9Number of occurrences of the word : 2
8Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry0
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
0#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
9Number of occurrences of the word : 2
8Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry0
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
0#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
9#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
5 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry5
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
7 Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry7
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry8
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry9
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry5
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
7 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 12
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 13
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 14
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 14
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 17
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 18
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry8
Occurrences of the word apple: 20
Occurrences of the word apple: 21
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 13
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 14
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4 mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 18
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
5 Occurrences of the word apple: 27
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
7 Occurrences of the word apple: 29
Total Number of Words: 150
#get file object reference to the file
file = open["C:\workspace\python\data.txt", "r"]
#read content of file to string
data = file.read[]
#get number of occurrences of the substring in the string
occurrences = data.count["python"]
print['Number of occurrences of the word :', occurrences]
9Total Number of Words: 152
Total Number of Words: 153
Mango banana apple pear Banana grapes strawberry Apple pear mango banana Kiwi apple mango strawberry1
Total Number of Words: 155
Output:
mango : 3 banana : 3 apple : 3 pear : 2 grapes : 1 strawberry : 2 kiwi : 1