Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận test.txt
Các tập tin văn bản bao gồm nội dung văn bản đơn giản. Tệp văn bản còn được gọi là tệp phẳng hoặc tệp đơn giản. Python cung cấp hỗ trợ dễ dàng để đọc và truy cập nội dung trong tệp. Các tệp văn bản được mở đầu tiên và sau đó nội dung được truy cập từ nó theo thứ tự các dòng. Theo mặc định, các số dòng bắt đầu với chỉ mục 0. Có nhiều cách khác nhau để đọc các dòng cụ thể từ một tệp văn bản trong Python, bài viết này nhằm mục đích thảo luận về chúng. & NBSP;
Tệp đang sử dụng: test.txt
Example:
Python3
Phương pháp 1: FileObject.ReadLines []
Một đối tượng tệp có thể được tạo trong python và sau đó readlines [] phương thức có thể được gọi trên đối tượng này để đọc các dòng vào một luồng. Phương pháp này được ưa thích khi một dòng hoặc một phạm vi dòng từ tệp cần được truy cập đồng thời. Nó có thể dễ dàng được sử dụng để in các dòng từ bất kỳ chỉ mục bắt đầu ngẫu nhiên nào sang một số chỉ mục kết thúc. Nó ban đầu đọc toàn bộ nội dung của tệp và giữ một bản sao của nó trong bộ nhớ. Các dòng tại các chỉ số được chỉ định sau đó được truy cập. & NBSP;
This is line 5.6
[
This is line 5.8
This is line 5.1
This is line 5.6
This is line 1. This is line 8. This is line 12.1
This is line 1. This is line 8. This is line 12.2
This is line 1. This is line 8. This is line 12.3
This is line 5.6
[
This is line 1. This is line 8. This is line 12.6
This is line 5.1
file
=
open
[
____1010
Output
This is line 5.2=
file
This is line 5.5
This is line 5.6This is line 1. This is line 8. This is line 12.10from itertools import islice # print the 100th line with open['the_file'] as lines: for line in islice[lines, 99, 100]: print line # print each third line until 100 with open['the_file'] as lines: for line in islice[lines, 0, 100, 3]: print line
1from itertools import islice # print the 100th line with open['the_file'] as lines: for line in islice[lines, 99, 100]: print line # print each third line until 100 with open['the_file'] as lines: for line in islice[lines, 0, 100, 3]: print line
2from itertools import islice # print the 100th line with open['the_file'] as lines: for line in islice[lines, 99, 100]: print line # print each third line until 100 with open['the_file'] as lines: for line in islice[lines, 0, 100, 3]: print line
This is line 1. This is line 8. This is line 12.3dòng thứ mười & nbsp;
Đây là dòng 10.
ba dòng đầu tiên & nbsp;
Đây là dòng 1. Đây là dòng 2. Đây là dòng 3.
Syntax:
getLine[txt-file, line_number]
Example:
Python3
Phương pháp 2: Gói Linecache & NBSP;
Gói linecache có thể được nhập vào Python và sau đó được sử dụng để trích xuất và truy cập các dòng cụ thể trong Python. Gói có thể được sử dụng để đọc đồng thời nhiều dòng. Nó sử dụng lưu trữ bộ đệm để thực hiện tối ưu hóa nội bộ. Gói này tự mở tệp và đến dòng cụ thể. Gói này có phương thức GetLine [] được sử dụng cho cùng. & NBSP;
This is line 5.6
with open['the_file'] as lines:
# print the first 100 lines
for line in islice[lines, 100]:
print line
# then skip the next 5
for line in islice[lines, 5]:
pass
# print the rest
for line in lines:
print line
4
4 from itertools import islice
# print the 100th line
with open['the_file'] as lines:
for line in islice[lines, 99, 100]:
print line
# print each third line until 100
with open['the_file'] as lines:
for line in islice[lines, 0, 100, 3]:
print line
5from itertools import islice
# print the 100th line
with open['the_file'] as lines:
for line in islice[lines, 99, 100]:
print line
# print each third line until 100
with open['the_file'] as lines:
for line in islice[lines, 0, 100, 3]:
print line
This is line 5.
6from itertools import islice
# print the 100th line
with open['the_file'] as lines:
for line in islice[lines, 99, 100]:
print line
# print each third line until 100
with open['the_file'] as lines:
for line in islice[lines, 0, 100, 3]:
print line
=
8from itertools import islice
# print the 100th line
with open['the_file'] as lines:
for line in islice[lines, 99, 100]:
print line
# print each third line until 100
with open['the_file'] as lines:
for line in islice[lines, 0, 100, 3]:
print line
This is line 5.
0
0with open['the_file'] as lines:
# print the first 100 lines
for line in islice[lines, 100]:
print line
# then skip the next 5
for line in islice[lines, 5]:
pass
# print the rest
for line in lines:
print line
1with open['the_file'] as lines:
# print the first 100 lines
for line in islice[lines, 100]:
print line
# then skip the next 5
for line in islice[lines, 5]:
pass
# print the rest
for line in lines:
print line
This is line 5.
1
Đầu ra:
Example:
Python3
Phương pháp 3: Enumate []
Phương thức liệt kê [] được sử dụng để chuyển đổi một chuỗi hoặc đối tượng danh sách thành chuỗi dữ liệu được lập chỉ mục theo số. Sau đó, nó được sử dụng trong danh sách dữ liệu kết hợp với FOR LOOP. Các dòng tại các chỉ mục cụ thể có thể được truy cập bằng cách chỉ định các số chỉ mục cần thiết trong một mảng. & Nbsp;
file
=
open
[
with open['the_file'] as lines:
# print the first 100 lines
for line in islice[lines, 100]:
print line
# then skip the next 5
for line in islice[lines, 5]:
pass
# print the rest
for line in lines:
print line
9This is line 5.1
# how to read lines #26 and #30
In [365]: list[islice[xrange[1,100], 25, 30, 4]]
Out[365]: [26, 30]
1=
# how to read lines #26 and #30
In [365]: list[islice[xrange[1,100], 25, 30, 4]]
Out[365]: [26, 30]
3from itertools import islice
# print the 100th line
with open['the_file'] as lines:
for line in islice[lines, 99, 100]:
print line
# print each third line until 100
with open['the_file'] as lines:
for line in islice[lines, 0, 100, 3]:
print line
0with open['the_file'] as lines:
# print the first 100 lines
for line in islice[lines, 100]:
print line
# then skip the next 5
for line in islice[lines, 5]:
pass
# print the rest
for line in lines:
print line
0# how to read lines #26 and #30
In [365]: list[islice[xrange[1,100], 25, 30, 4]]
Out[365]: [26, 30]
6____40# how to read lines #26 and #30
In [365]: list[islice[xrange[1,100], 25, 30, 4]]
Out[365]: [26, 30]
8# how to read lines #26 and #30
In [365]: list[islice[xrange[1,100], 25, 30, 4]]
Out[365]: [26, 30]
9=
2
This is line 5.6
=
4file
0 file
1file
2 file
3[
file
____6666
This is line 1. This is line 8. This is line 12.
Để hoàn thiện, đây là một lựa chọn nữa.
Hãy bắt đầu với một định nghĩa từ Python Docs:
Cắt một đối tượng thường chứa một phần của một chuỗi. Một lát cắt được tạo bằng cách sử dụng ký hiệu phụ, [] với các dấu chấm giữa các số khi một số được đưa ra, chẳng hạn như trong biến_name [1: 3: 5]. Ký hiệu khung [chỉ số] sử dụng các đối tượng lát bên trong [hoặc trong các phiên bản cũ hơn, __getSlice __ [] và __setslice __ []]. An object usually containing a portion of a sequence. A slice is created using the subscript notation, [] with colons between numbers when several are given, such as in variable_name[1:3:5]. The bracket [subscript] notation uses slice objects internally [or in older versions, __getslice__[] and __setslice__[]].
Mặc dù ký hiệu lát cắt không được áp dụng trực tiếp cho các trình lặp nói chung, gói =
5 chứa chức năng thay thế:
from itertools import islice
# print the 100th line
with open['the_file'] as lines:
for line in islice[lines, 99, 100]:
print line
# print each third line until 100
with open['the_file'] as lines:
for line in islice[lines, 0, 100, 3]:
print line
Ưu điểm bổ sung của hàm là nó không đọc trình lặp cho đến khi kết thúc. Vì vậy, bạn có thể làm những điều phức tạp hơn:
with open['the_file'] as lines:
# print the first 100 lines
for line in islice[lines, 100]:
print line
# then skip the next 5
for line in islice[lines, 5]:
pass
# print the rest
for line in lines:
print line
Và để trả lời câu hỏi ban đầu:
# how to read lines #26 and #30
In [365]: list[islice[xrange[1,100], 25, 30, 4]]
Out[365]: [26, 30]