Nếu bạn chỉ muốn một từ tại một thời điểm (bỏ qua ý nghĩa của khoảng không gian so với ngắt dòng trong tệp):
Tương tự, nếu bạn muốn làm phẳng tệp vào một danh sách các từ trong tệp, bạn có thể làm điều gì đó như thế này:
Có thể tạo ra đầu ra giống như ví dụ đầu tiên với ____ 13 ...
Hoặc, nếu bạn muốn một danh sách các từ trong mỗi dòng của tệp (ví dụ: để tạo một ma trận hàng và cột từ một tệp):
Ví dụ: hãy để Lừa tìm ra các từ dài nhất trong một tệp có tên là Astr Acress.txt,:
Nếu bạn bối rối, chức năng with open("words.txt") as f:
(word for line in f for word in re.findall(r'\w+', line))
5:
ĐọcBàn luậnCải thiện bài viết
Lưu bài viết
Đ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 và nhiệm vụ là đọc thông tin từ tệp từng chữ trong Python.
Examples:
Đầu vào: Tôi là R2J! Đầu ra: iamr2j!
I am R2J!
Output:
I
am
R2J!
Đầu vào: Geek 4 Geeksand Trong giấc mơ đó, chúng tôi đã bay.
Geeks 4 Geeks
And in that dream, we were flying.
Output:
Geeks
4
Geeks
And
in
that
dream,
we
were
flying.
Approach:
- Mở một tệp trong chế độ đọc có chứa một chuỗi.
- Sử dụng vòng lặp
with open('words.txt','r') as f:
for line in f:
for word in line.split():
print(word)
6 để đọc từng dòng từ tệp văn bản. - Một lần nữa sử dụng vòng lặp
with open('words.txt','r') as f:
for line in f:
for word in line.split():
print(word)
6 để đọc từng từ từ dòng được chia bằng ‘. - Hiển thị từng từ từ mỗi dòng trong tệp văn bản.
Ví dụ 1: Hãy giả sử tệp văn bản trông như thế này - Let’s suppose the text file looks like this –
Tệp văn bản:
with open('words.txt','r') as f:
for line in f:
for word in line.split():
print(word)
8with open('words.txt','r') as f:
for line in f:
for word in line.split():
print(word)
9line1
word1
word2
line2
...
word6
0line1
word1
word2
line2
...
word6
1line1
word1
word2
line2
...
word6
2line1
word1
word2
line2
...
word6
3line1
word1
word2
line2
...
word6
4line1
word1
word2
line2
...
word6
5line1
word1
word2
line2
...
word6
6line1
word1
word2
line2
...
word6
7with open('words.txt','r') as f:
for line in f:
for word in line.split():
print(word)
6 line1
word1
word2
line2
...
word6
9with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
0 line1
word1
word2
line2
...
word6
5line1
word1
word2
line2
...
word6
6with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
3with open('words.txt','r') as f:
for line in f:
for word in line.split():
print(word)
6 with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
5with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
0 with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
7with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
8with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
9with open('words.txt') as f:
matrix=[line.split() for line in f]
>>> matrix
[['line1', 'word1', 'word2'], ['line2', 'word3', 'word4'], ['line3', 'word5', 'word6']]
0Output:
Geeks
4
geeks
Ví dụ 2: Hãy giả sử tệp văn bản chứa nhiều dòng. Let’s suppose the text file contains more than one line.
Tệp văn bản:
with open('words.txt','r') as f:
for line in f:
for word in line.split():
print(word)
8with open('words.txt','r') as f:
for line in f:
for word in line.split():
print(word)
9line1
word1
word2
line2
...
word6
0line1
word1
word2
line2
...
word6
1line1
word1
word2
line2
...
word6
2line1
word1
word2
line2
...
word6
3line1
word1
word2
line2
...
word6
4line1
word1
word2
line2
...
word6
5line1
word1
word2
line2
...
word6
6line1
word1
word2
line2
...
word6
7with open('words.txt','r') as f:
for line in f:
for word in line.split():
print(word)
6 line1
word1
word2
line2
...
word6
9with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
0 line1
word1
word2
line2
...
word6
5line1
word1
word2
line2
...
word6
6with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
3with open('words.txt','r') as f:
for line in f:
for word in line.split():
print(word)
6 with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
5with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
0 with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
7with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
8with open('words.txt') as f:
flat_list=[word for line in f for word in line.split()]
>>> flat_list
['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6']
9with open('words.txt') as f:
matrix=[line.split() for line in f]
>>> matrix
[['line1', 'word1', 'word2'], ['line2', 'word3', 'word4'], ['line3', 'word5', 'word6']]
0Output:
Geeks
4
Geeks
And
in
that
dream,
we
were
flying.
Ví dụ: hãy để Lừa tìm ra các từ dài nhất trong một tệp có tên là Astr Acress.txt,:
Ngoài ra, tìm hiểu thêm về các toàn bộ danh sách để tạo tốc ký cho các vòng lặp như hình trong dòng cuối cùng thứ hai.
Ví dụ: hãy để Lừa tìm ra các từ dài nhất trong một tệp có tên là Astr Acress.txt,:
Ngoài ra, tìm hiểu thêm về các toàn bộ danh sách để tạo tốc ký cho các vòng lặp như hình trong dòng cuối cùng thứ hai.