Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
- Trong bài viết này, chúng tôi sẽ tạo ra một chương trình Python đếm nguyên âm, dòng và một số ký tự có trong một tệp văn bản cụ thể.open() function in python.
- Cách tiếp cận
- Chúng ta phải mở tệp bằng hàm Open () trong Python.
- Sau đó tạo ba biến, nguyên âm, dòng và ký tự để đếm số lượng nguyên âm, dòng và ký tự tương ứng.
- Lập danh sách các nguyên âm để chúng ta có thể kiểm tra xem ký tự có phải là nguyên âm hay không.
Khi số đếm đạt nhân vật & nbsp; ‘\ n, chúng ta phải tăng biến dòng của chúng ta có nghĩa là một dòng mới trong tệp.
Python3
Sau đó lặp lại các ký tự của tệp và đếm các nguyên âm, dòng và ký tự.
Dưới đây là triển khai đầy đủ:
def counting(filename):
Các
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 213= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 215
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 217= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 215
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 211= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 215
Các
Các
0 1 2 3
4 5 1 2 8
9Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 213txt_file 1= txt_file 3
4txt_file 5 1txt_file 7 2 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 215=0
9Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 211txt_file 1= txt_file 3
(filename, 3(filename, 4(filename, 5(filename, 6(filename, 7(filename, 8
4txt_file 5 1== =3=4
(filename, 3(filename, 4Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2109(filename, 6(filename, 7Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2112
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2113Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2114Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 211
Output:
Number of vowels in MyFile.txt = 23 New Lines in MyFile.txt = 2 Number of characters in MyFile.txt = 549Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 217txt_file 1= txt_file 3
Điều kiện tiên quyết: Xử lý tập tin trong Python File Handling in Python
Đưa ra một tệp văn bản fname, nhiệm vụ là đếm tổng số ký tự, từ, khoảng trắng và dòng trong tệp. Như chúng ta đã biết, Python cung cấp nhiều tính năng và mô-đun được xây dựng để xử lý các tệp. Hãy để thảo luận về các cách khác nhau để tính tổng số ký tự, từ, khoảng trắng và dòng trong một tệp sử dụng Python. & NBSP;
Nhận số lượng ký tự, từ, khoảng cách và dòng trong một tệp
Cách tiếp cận ngây thơ để có được số lượng ký tự, từ, khoảng trống và dòng trong một tệp
Trong cách tiếp cận này, ý tưởng là giải quyết nhiệm vụ bằng cách phát triển logic của chúng ta. Không sử dụng bất kỳ chức năng tích hợp nào của Python, tổng số ký tự, từ, khoảng trắng và dòng của tệp sẽ được tính toán. Dưới đây là việc thực hiện phương pháp trên. & NBSP;
Python3
def Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2117
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2119__ Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 215
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2123= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 215
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2127= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 215
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2131= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 215
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2135openNumber of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2137Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2138Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2139
4 0 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 217 2 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2144
9Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2123txt_file 1= txt_file 3
9Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2151= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2153
9 0 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2156 2 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2158
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2159 5 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2161= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2163 =0 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2151__
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2170Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2119txt_file 1= txt_file 3
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2170Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2151= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2178
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2159txt_file 5 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2181=__
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2170Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2131txt_file 1= txt_file 3
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2170Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2151= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2153
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2159 0 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2197 2 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2199
Các
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2110Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2127txt_file 1= txt_file 3
(filename, 3(filename, 4Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2118def7
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2120Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2121
(filename, 3(filename, 4Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2125def7
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2120Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2128
(filename, 3(filename, 4Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2132def7
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2120Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2135
(filename, 3(filename, 4Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2139def7
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2120Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2142
5 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2144== Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2147=4
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2150__ Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2152
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2154=4
4Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2157
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2159=4
4(filename, 3(filename, 4Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2164Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 211
Output:
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 21Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2154=4
Điều kiện tiên quyết: Xử lý tập tin trong Python
Python3
Đưa ra một tệp văn bản fname, nhiệm vụ là đếm tổng số ký tự, từ, khoảng trắng và dòng trong tệp. Như chúng ta đã biết, Python cung cấp nhiều tính năng và mô-đun được xây dựng để xử lý các tệp. Hãy để thảo luận về các cách khác nhau để tính tổng số ký tự, từ, khoảng trắng và dòng trong một tệp sử dụng Python. & NBSP;
def Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2117
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2119__ Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 215
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2123= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 215
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2127= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 215
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2131= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 215
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2135openNumber of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2137Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2138Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2139
4 0 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 217 2 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2144
9Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2123txt_file 1= txt_file 3
9Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2151= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2153
9Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2123= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2123txt_file 1 txt_file 3
9Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2119= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2119txt_file 1 def16def17
9Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2127= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2127txt_file 1 def23__
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2110 5 def27txt_file 7 2 def35Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2163__
9Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2131= Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2131txt_file 1
def50 5 def47 2 def35Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2163def37
(filename, 3(filename, 4Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2118def7
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2120Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2121
(filename, 3(filename, 4Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2125def7
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2120Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2128
(filename, 3(filename, 4def74def7
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2120Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2135
(filename, 3(filename, 4def81def7
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2120Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2142
5 Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2144== Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2147=4
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2150__ Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2152
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2154=4
4Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2157
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2159=4
4(filename, 3(filename, 4Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 2164Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 211
Output:
Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 21