Đ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
counter[fname]:
num_words
=
0
num_lines
=
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: 213
=
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
=
0
def
1def
2def
3def
4def
5
def
6def
7 def
8def
9 counter[fname]:
0
counter[fname]:
1num_lines
counter[fname]:
3=
counter[fname]:
5
counter[fname]:
1counter[fname]:
7=
counter[fname]:
9
counter[fname]:
1def
7
2def
9
4
5
6
7=
9 num_words
0 counter[fname]:
7=
__
num_words
6num_words
counter[fname]:
3=
counter[fname]:
5
num_words
6counter[fname]:
7=
=
4
5=
6 =
7=
=
9num_words
5
num_words
6
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
counter[fname]:
3=
counter[fname]:
5num_words
6counter[fname]:
7=
counter[fname]:
9
5def
7
3def
9
5
num_words
6
6
8=
num_lines
0 num_words
0 num_lines
2________num_lines
4____num_words
5
num_lines
6
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
counter[fname]:
3=
counter[fname]:
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: 2102
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: 2103
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: 2104
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: 2105
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: 2106
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: 2107
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: 2102
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: 2103
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: 2111
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: 2105
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: 2106
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: 2114
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: 2102
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: 2103
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: 2118
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: 2105
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: 2106
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: 2121
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: 2102
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: 2103
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: 2125
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: 2105
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: 2106
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: 2128
6
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: 2130
=
=
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: 2133
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: 2134
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: 2136
=
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: 2138
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: 2140
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: 2134
def
6
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: 2143
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: 2145
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: 2134
def
6
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: 2102
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: 2103
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: 2151
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
Sử dụng một số chức năng tích hợp và chức năng mô-đun hệ điều hành
Theo cách tiếp cận này, ý tưởng là sử dụng phương thức mô -đun HĐH OS.LINEEP [] để tách các dòng trên nền tảng hiện tại. Khi máy quét phiên dịch cuộc gặp gỡ OS.LINEEP, nó thay thế nó bằng ký tự \ n. Sau các hàm Dải [] và Split [] sẽ được sử dụng để thực hiện nhiệm vụ. Nhận thêm ý tưởng về các hàm Dải [] và Split []. Dưới đây là việc thực hiện phương pháp trên. & NBSP;
Python3
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: 2153
def
counter[fname]:
num_words
=
0
num_lines
=
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: 213
=
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
=
0
def
1def
2def
3def
4def
5
def
6def
7 def
8def
9 counter[fname]:
0
counter[fname]:
1num_lines
counter[fname]:
3=
counter[fname]:
5
counter[fname]:
1counter[fname]:
7=
counter[fname]:
9
counter[fname]:
1def
7
2def
9
4
5
6
7=
9 num_words
0 counter[fname]:
7=
__
num_words
6num_words
counter[fname]:
3=
counter[fname]:
5
num_words
6counter[fname]:
7=
=
4
5=
6 =
7=
=
9num_words
5
num_words
6
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
counter[fname]:
3=
counter[fname]:
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: 2102
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: 2103
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: 2104
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: 2105
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: 2106
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: 2107
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: 2102
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: 2103
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: 2111
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: 2105
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: 2106
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: 2114
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: 2102
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: 2103
def
60Number 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: 2105
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: 2106
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: 2121
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: 2102
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: 2103
def
67Number 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: 2105
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: 2106
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: 2128
6
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: 2130
=
=
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: 2133
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: 2134
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: 2136
=
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: 2138
Sử dụng một số chức năng tích hợp và chức năng mô-đun hệ điều hành
def
6
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: 2143
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: 2145
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: 2134
def
6
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: 2102
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: 2103
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: 2151
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