Hướng dẫn how do you count the number of times a word appears in a file python? - làm cách nào để đếm số lần một từ xuất hiện trong tệp python?

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 đó

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
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.

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 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]
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 strawberry
7

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.
3
Welcome 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.
5
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
7
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
8
Welcome 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]
1
Welcome 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]
6
Welcome 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]
6
Welcome 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]
9
Number of occurrences of the word : 2
8
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
0
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]
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 strawberry
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]
7
Mango 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
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
9
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
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]
7
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
2

mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
3
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
4
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 : 1
4
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
7
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
8

Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
8
Occurrences of the word apple: 2
0
Occurrences of the word apple: 2
1

mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
3
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
4
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 : 1
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]
5
Occurrences of the word apple: 2
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]
7
Occurrences of the word apple: 2
9
Total Number of Words: 15
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
Total Number of Words: 15
2
Total Number of Words: 15
3
Total Number of Words: 15
4
Total Number of Words: 15
5

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 strawberry
5
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
4
Total Number of Words: 15
8

Total Number of Words: 15
9
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.
5
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6
Mango! 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.
8
Mango! 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 : 1
3

Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
8
Number of occurrences of the word : 2
8
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 : 1
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]
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 strawberry
7

mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
3
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
9
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
05
Welcome 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.
4
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
08

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
09
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
10
Welcome 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.
10
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
7
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
8

Total Number of Words: 15
2
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
17
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
18
Total Number of Words: 15
4
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
20

Output:

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: 15
9
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 : 1
3

Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
8
Number of occurrences of the word : 2
8
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 : 1
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]
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 strawberry
7

Total Number of Words: 15
2
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
17
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
18
Total Number of Words: 15
4
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
20

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

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
54

Output:

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.
24
Welcome 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.
5
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
7
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
8
Welcome 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

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3
Welcome 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.
5
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
7
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
8
Welcome 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]
1
Welcome 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]
6
Welcome 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]
6
Welcome 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]
9
Number of occurrences of the word : 2
8
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
0
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]
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
Number of occurrences of the word : 2
8
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
0
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]
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 strawberry
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]
7
Mango 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
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
9
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
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]
7
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
2

mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
3
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
4
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 : 1
4
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
7
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
8

Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
8
Occurrences of the word apple: 2
0
Occurrences of the word apple: 2
1

mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
3
mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1
4
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 : 1
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]
5
Occurrences of the word apple: 2
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]
7
Occurrences of the word apple: 2
9
Total Number of Words: 15
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
Total Number of Words: 15
2
Total Number of Words: 15
3
Mango banana apple pear
Banana grapes strawberry
Apple pear mango banana
Kiwi apple mango strawberry
1
Total Number of Words: 15
5

Output:

mango : 3
banana : 3
apple : 3
pear : 2
grapes : 1
strawberry : 2
kiwi : 1

Làm thế nào để bạn đếm số lần một chuỗi xuất hiện trong một tệp python?

Phương thức Python String Count [] Phương thức Đếm [] trả về số lần một giá trị được chỉ định xuất hiện trong chuỗi.count[] Method The count[] method returns the number of times a specified value appears in the string.

Làm thế nào để bạn đếm số lần xuất hiện của một từ trong một tệp?

Chỉ sử dụng grep -c sẽ đếm số lượng dòng chứa từ phù hợp thay vì số lượng tổng số trận đấu.Tùy chọn -O là những gì bảo GRP xuất mỗi trận đấu theo một dòng duy nhất và sau đó WC -L bảo WC đếm số lượng dòng.Đây là cách tổng số từ các từ phù hợp được suy ra.. The -o option is what tells grep to output each match in a unique line and then wc -l tells wc to count the number of lines. This is how the total number of matching words is deduced.

Làm cách nào để đếm số lần một từ xuất hiện trong một tệp văn bản?

Để đế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 [].read the content of text file to a string and use String. count[] function with the word passed as argument to the count[] function.

Bài Viết Liên Quan

Chủ Đề