Có thể đọc từ một tệp văn bản, một phạm vi dòng đặt ví dụ từ dòng 20 đến 52 không?
nhưng chỉ muốn lưu dữ liệu trong một phạm vi đã đặt hoặc nếu có thể đọc từ một dòng sau một dòng chứa --- dữ liệu --- sang một dòng khác có chứa-kết thúc dữ liệu-
Tôi đã xem qua các tài liệu và các ví dụ trực tuyến và chỉ có thể tìm thấy các ví dụ chỉ định một dòng mới hoặc một cái gì đó
sẽ đọc các dòng từ 20 đến 52; Python sử dụng lập chỉ mục dựa trên 0, vì vậy dòng 1 được đánh số 0.
Sử dụng đối tượng tệp làm trình lặp cung cấp cho bạn rất nhiều tính linh hoạt; Bạn có thể đọc các dòng bổ sung bằng cách gọi next()
trên đối tượng tệp, ví dụ, nâng cao dòng 'con trỏ' khi bạn làm như vậy.
Bạn có thể làm hai điều. Bạn có thể sử dụng
HM, bạn có thể thử một cái gì đó trong khi vòng lặp .... nhưng bạn nên biết chuỗi nào bạn muốn văn bản được tải, đó không phải là cách tốt nhất:
Cải thiện bài viết
Lưu bài viết
Tệp 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 test.txt
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;
Example:
Python3
import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
4 import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
5 import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
6import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
7import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
8import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
9text_file = open(file_to_save, "r")
lines = []
for index, text in enumerate(text_file):
if 19 <= index <= 51:
lines.append(text)
0import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
5 import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
4text_file = open(file_to_save, "r")
lines = []
for index, text in enumerate(text_file):
if 19 <= index <= 51:
lines.append(text)
3text_file = open(file_to_save, "r")
lines = []
for index, text in enumerate(text_file):
if 19 <= index <= 51:
lines.append(text)
4import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
7text_file = open(file_to_save, "r")
lines = []
for index, text in enumerate(text_file):
if 19 <= index <= 51:
lines.append(text)
6import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
9text_file = open(file_to_save, "r")
lines = []
for index, text in enumerate(text_file):
if 19 <= index <= 51:
lines.append(text)
4text_file = open(file_to_save, "r")
lines = []
for index, text in enumerate(text_file):
if 19 <= index <= 51:
lines.append(text)
9text_file = open(file_to_save, "r")
lines = text_file.readlines()
lines = lines[19:52]
0text_file = open(file_to_save, "r")
lines = text_file.readlines()
lines = lines[19:52]
1text_file = open(file_to_save, "r")
lines = []
for index, text in enumerate(text_file):
if 19 <= index <= 51:
lines.append(text)
4import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
7text_file = open(file_to_save, "r")
lines = text_file.readlines()
lines = lines[19:52]
4import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
9text_file = open(file_to_save, "r")
lines = []
for index, text in enumerate(text_file):
if 19 <= index <= 51:
lines.append(text)
4text_file = open(file_to_save, "r")
lines = []
for index, text in enumerate(text_file):
if 19 <= index <= 51:
lines.append(text)
9text_file = open(file_to_save, "r")
lines = text_file.readlines()
lines = lines[19:52]
8text_file = open(file_to_save, "r")
lines = text_file.readlines()
lines = lines[19:52]
9text_file=open(name,'r')
line=text.readline()
while line!=(" ") #type the line where you want it to stop
print (line
0text_file = open(file_to_save, "r")
lines = text_file.readlines()
lines = lines[19:52]
1Output
dò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.
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;
Syntax:
getLine(txt-file, line_number)
Example:
Python3
text_file=open(name,'r')
line=text.readline()
while line!=(" ") #type the line where you want it to stop
print (line
2 text_file=open(name,'r')
line=text.readline()
while line!=(" ") #type the line where you want it to stop
print (line
3text_file=open(name,'r')
line=text.readline()
while line!=(" ") #type the line where you want it to stop
print (line
4import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
5 text_file=open(name,'r')
line=text.readline()
while line!=(" ") #type the line where you want it to stop
print (line
6import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
8text_file=open(name,'r')
line=text.readline()
while line!=(" ") #type the line where you want it to stop
print (line
8text_file=open(name,'r')
line=text.readline()
while line!=(" ") #type the line where you want it to stop
print (line
9import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
9text_file = open(file_to_save, "r")
lines = []
for index, text in enumerate(text_file):
if 19 <= index <= 51:
lines.append(text)
4getLine(txt-file, line_number)
2Đầu ra:
This is line 5.
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;
Example:
Python3
import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
4 import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
5 import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
6import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
7getLine(txt-file, line_number)
7import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
9getLine(txt-file, line_number)
9import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
5 This is line 5.
1text_file = open(file_to_save, "r")
lines = text_file.readlines()
lines = lines[19:52]
8text_file=open(name,'r')
line=text.readline()
while line!=(" ") #type the line where you want it to stop
print (line
8This is line 5.
44444866666This is line 5.
8 This is line 5.
9This is line 1.
This is line 8.
This is line 12.
0 This is line 1.
This is line 8.
This is line 12.
1import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
7import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
4This is line 1.
This is line 8.
This is line 12.
4This is line 1.
This is line 8.
This is line 12.
5import itertools
with open(file_to_save, "r") as text_file:
for line in itertools.islice(text_file, 19, 52):
# do something with line
2 This is line 1.
This is line 8.
This is line 12.
77____70 This is line 1.
This is line 8.
This is line 12.
9itertools.islice()
0
text_file = open(file_to_save, "r")
lines = []
for index, text in enumerate(text_file):
if 19 <= index <= 51:
lines.append(text)
4itertools.islice()
2Đầu ra
This is line 1.
This is line 8.
This is line 12.