Bạn có thể đếm số từ trong một tệp văn bản, trước tiên bằng cách đọc văn bản cho một biến, sau đó đếm các ký tự. Chúng ta sẽ đi qua trình tự các bước cần thiết để đếm các ký tự
Các bước để đếm số ký tự
Để đếm số ký tự trong một tệp văn bản, hãy làm theo các bước sau
- Mở tệp ở chế độ đọc
- Đọc văn bản bằng hàm read[]
- Lấy độ dài của chuỗi, đó phải là số ký tự trong tệp văn bản
- Bạn có thể tinh chỉnh số lượng bằng cách làm sạch chuỗi như xóa các ký tự khoảng trắng và dấu chấm câu
ví dụ 1. Đếm các ký tự trong một tệp văn bản
Trong ví dụ Python này, chúng ta sẽ đọc một tệp văn bản và đếm số ký tự trong đó. Xét tệp văn bản sau
Tệp văn bản
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
Chương trình Python
#open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
đầu ra
Number of characters in text file : 97
ví dụ 2. Đếm ký tự trong tệp văn bản không bao gồm khoảng trắng
Trong ví dụ về Python này, chúng ta sẽ đọc một tệp văn bản và đếm số ký tự trong đó không bao gồm các ký tự khoảng trắng. Xét tệp văn bản sau
Tệp văn bản
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
Chương trình Python
#open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file and replace spaces with nothing
data = file.read[].replace[" ",""]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
đầu ra
Number of characters in text file : 84
Tóm lược
Trong hướng dẫn về Ví dụ Python này, chúng ta đã học cách đếm số ký tự trong tệp văn bản, với sự trợ giúp của các chương trình ví dụ
Hướng dẫn này sẽ giới thiệu cách đếm số lần xuất hiện của một ký tự trong Chuỗi trong Python
Sử dụng Hàm ________ 31 để đếm số lượng ký tự xuất hiện trong một chuỗi trong Python
Chúng ta có thể đếm số lần xuất hiện của một giá trị trong chuỗi bằng cách sử dụng hàm
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1. Nó sẽ trả về số lần giá trị xuất hiện trong chuỗi đã choVí dụ,
print['Mary had a little lamb'.count['a']]
đầu ra
Hãy nhớ rằng, chữ hoa và chữ thường được coi là các ký tự khác nhau.
from collections import Counter
my_str = "Mary had a little lamb"
counter = Counter[my_str]
print[counter['a']]
1 và from collections import Counter
my_str = "Mary had a little lamb"
counter = Counter[my_str]
print[counter['a']]
2 sẽ được coi là các ký tự khác nhau và có số lượng khác nhauSử dụng from collections import Counter
my_str = "Mary had a little lamb"
counter = Counter[my_str]
print[counter['a']]
3 để đếm số lần xuất hiện của một ký tự trong một chuỗi bằng Python
from collections import Counter
my_str = "Mary had a little lamb"
counter = Counter[my_str]
print[counter['a']]
Một
from collections import Counter
my_str = "Mary had a little lamb"
counter = Counter[my_str]
print[counter['a']]
4 là một phân lớp từ điển có trong mô-đun from collections import Counter
my_str = "Mary had a little lamb"
counter = Counter[my_str]
print[counter['a']]
5. Nó lưu trữ các phần tử dưới dạng khóa từ điển và các lần xuất hiện của chúng được lưu trữ dưới dạng giá trị từ điển. Thay vì tăng lỗi, nó trả về số 0 cho các mục bị thiếuVí dụ,
from collections import Counter
my_str = "Mary had a little lamb"
counter = Counter[my_str]
print[counter['a']]
đầu ra
Đó là một lựa chọn tốt hơn khi đếm nhiều chữ cái vì bộ đếm tính toán tất cả các lần đếm một lần. Nó nhanh hơn rất nhiều so với hàm
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1Sử dụng Biểu thức chính quy để đếm số lần xuất hiện của một ký tự trong chuỗi bằng Python
Biểu thức chính quy là một cú pháp chuyên biệt được giữ trong một mẫu giúp tìm các chuỗi hoặc tập hợp các chuỗi bằng cách khớp với mẫu đó. Chúng tôi nhập mô-đun
from collections import Counter
my_str = "Mary had a little lamb"
counter = Counter[my_str]
print[counter['a']]
7 để hoạt động với các biểu thức chính quyChúng ta có thể sử dụng hàm
from collections import Counter
my_str = "Mary had a little lamb"
counter = Counter[my_str]
print[counter['a']]
8 cho vấn đề của mìnhVí dụ,
#open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
5đầu ra
Sử dụng from collections import Counter
my_str = "Mary had a little lamb"
counter = Counter[my_str]
print[counter['a']]
9 để đếm số lần xuất hiện của một ký tự trong một chuỗi bằng Python
from collections import Counter
my_str = "Mary had a little lamb"
counter = Counter[my_str]
print[counter['a']]
#open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
50 có mặt trong mô-đun from collections import Counter
my_str = "Mary had a little lamb"
counter = Counter[my_str]
print[counter['a']]
5 và được lấy từ lớp từ điển. Chức năng của nó tương đối giống với chức năng của từ điển ngoại trừ việc nó không bao giờ tăng giá trị #open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
52, vì nó cung cấp một giá trị mặc định cho khóa không bao giờ tồn tạiChúng ta có thể sử dụng nó để lấy số lần xuất hiện của một ký tự trong chuỗi như hình bên dưới
Number of characters in text file : 97
0đầu ra
Sử dụng #open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
53 để đếm số lần xuất hiện của một ký tự trong chuỗi bằng Python
#open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
Chúng ta có thể sử dụng phương thức
#open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
53 để lấy số lần xuất hiện của tất cả các ký tự có trong chuỗi được cung cấp. Chúng ta cần truyền chuỗi dưới dạng đối tượng #open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
55Ví dụ,
Number of characters in text file : 97
4đầu ra
Number of characters in text file : 97
5Nó trả về sự xuất hiện của tất cả các ký tự trong một đối tượng
#open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
55Sử dụng Biểu thức #open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
57 để đếm số lần xuất hiện của một ký tự trong chuỗi bằng Python
#open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
Các hàm
#open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
57 không chỉ có thể đếm số lần xuất hiện từ chuỗi đã cho mà còn có thể hoạt động khi chúng ta có chuỗi, dưới dạng danh sách các chuỗi conXem đoạn mã sau
Number of characters in text file : 97
9đầu ra
Sử dụng Vòng lặp #open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
59 để đếm số lần xuất hiện của một ký tự trong một chuỗi bằng Python
#open file in read mode
file = open["C:\data.txt", "r"]
#read the content of file
data = file.read[]
#get the length of the data
number_of_characters = len[data]
print['Number of characters in text file :', number_of_characters]
Chúng tôi lặp qua chuỗi và nếu phần tử bằng với ký tự mong muốn, biến đếm được tăng lên cho đến khi chúng tôi đến cuối chuỗi