Hướng dẫn extract specific line from text file python - trích xuất dòng cụ thể từ tệp văn bản python

1. Cố gắng đọc toàn bộ tệp

Một tốc độ bạn có thể làm là đọc toàn bộ tệp trong bộ nhớ nếu điều đó là có thể, nếu không hãy đọc bằng các đoạn. Bạn đã nói 'một số dòng hàng nghìn đường' giả sử 1 triệu dòng với mỗi dòng 100 char, tức là khoảng 100 MB, nếu bạn có nhiều bộ nhớ miễn phí đó [tôi cho rằng bạn có] chỉ cần làm điều này

big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
big_file.close[]
small_file3 = open['C:\\small_file3.txt', 'w']
for line in big_file_lines:
   if 'S0414' in line:
      small_file3.write[line]
small_file3.close[]

Thời gian này với phiên bản Orginal và xem nó có tạo nên sự khác biệt không, tôi nghĩ nó sẽ như vậy.

Nhưng nếu tập tin của bạn thực sự lớn trong GBS, thì bạn có thể đọc nó bằng các đoạn, ví dụ: 100 MB, chia nó thành các dòng và tìm kiếm nhưng đừng quên tham gia các dòng ở mỗi khoảng 100MB [tôi có thể giải thích nhiều hơn nếu đây là trường hợp]

File.ReadLines Trả về một danh sách chứa tất cả các dòng dữ liệu trong tệp. Nếu được cung cấp một tham số tùy chọn sizehint, nó sẽ đọc rằng nhiều byte từ tệp và đủ nhiều hơn để hoàn thành một dòng và trả về các dòng từ đó. Điều này thường được sử dụng để cho phép đọc hiệu quả một tệp lớn theo các dòng, nhưng không phải tải toàn bộ tệp trong bộ nhớ. Chỉ các dòng hoàn chỉnh sẽ được trả lại.This is often used to allow efficient reading of a large file by lines, but without having to load the entire file in memory. Only complete lines will be returned.

Cũng xem liên kết sau để biết sự khác biệt về tốc độ giữa các dòng theo dòng so với toàn bộ việc đọc tệp.

2. Cố gắng viết toàn bộ tệp

Bạn cũng có thể lưu trữ dòng và viết chúng cùng một lúc, mặc dù tôi không chắc nó có giúp ích nhiều

big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]

3. Thử bộ lọc

Bạn cũng có thể thử sử dụng bộ lọc, thay vì vòng lặp xem nó có tạo ra sự khác biệt không

small_file_lines= filter[lambda line:line.find['S0414'] >= 0, big_file_lines]

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

file = open[____1010

big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
2= file
big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
5

big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
6[
big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
8
big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
1

big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
6
small_file_lines= filter[lambda line:line.find['S0414'] >= 0, big_file_lines]
1
small_file_lines= filter[lambda line:line.find['S0414'] >= 0, big_file_lines]
2
small_file_lines= filter[lambda line:line.find['S0414'] >= 0, big_file_lines]
3

big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
6[
small_file_lines= filter[lambda line:line.find['S0414'] >= 0, big_file_lines]
6
big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
1

big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
6
small_file_lines= filter[lambda line:line.find['S0414'] >= 0, big_file_lines]
1
getLine[txt-file, line_number]
0
getLine[txt-file, line_number]
1
getLine[txt-file, line_number]
2
small_file_lines= filter[lambda line:line.find['S0414'] >= 0, big_file_lines]
3

Output 

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

getLine[txt-file, line_number]
4
getLine[txt-file, line_number]
5

getLine[txt-file, line_number]
6=
getLine[txt-file, line_number]
8
big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
0
This is line 5.
0
This is line 5.
1
big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
1

big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
6
This is line 5.
4

Đầ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

file = open[

This is line 5.
9
big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
1

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.
3
getLine[txt-file, line_number]
0
This is line 5.
0
This is line 1.
This is line 8.
This is line 12.
6____40
This is line 1.
This is line 8.
This is line 12.
8
This is line 1.
This is line 8.
This is line 12.
9

file0 file1file2 file3[file____6666

file7file8 file9file2 =1

=2

big_file = open['C:\\gbigfile.txt', 'r']
big_file_lines = big_file.read_lines[]
small_file_lines = []
for line in big_file_lines:
   if 'S0414' in line:
      small_file_lines.append[line]
small_file3 = open['C:\\small_file3.txt', 'w']
small_file3.write["".join[small_file_lines]]
small_file3.close[]
6=4

Đầu ra

This is line 1.
This is line 8.
This is line 12.

Bài Viết Liên Quan

Chủ Đề