Danh sách xây dựng các dòng phù hợp - một số hương vị:
def lines_that_equal[line_to_match, fp]:
return [line for line in fp if line == line_to_match]
def lines_that_contain[string, fp]:
return [line for line in fp if string in line]
def lines_that_start_with[string, fp]:
return [line for line in fp if line.startswith[string]]
def lines_that_end_with[string, fp]:
return [line for line in fp if line.endswith[string]]
Build Trình tạo các dòng phù hợp [bộ nhớ hiệu quả]:
def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
In tất cả các dòng đối sánh [tìm tất cả các trận đấu trước, sau đó in chúng]:
with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
In tất cả các dòng phù hợp [in chúng một cách lười biếng, như chúng ta tìm thấy chúng]
with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
Máy phát điện [được sản xuất bởi năng suất] là bạn bè của bạn, đặc biệt là với các tệp lớn không phù hợp với bộ nhớ.
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
Example:
string = "GEEK FOR GEEKS" Input: "FOR" Output: Yes, FOR is present in the given string.
Bàn luận
myfile.txt
Trong bài viết này, chúng ta sẽ xem cách tìm kiếm một chuỗi trong các tệp văn bản bằng Python
Tệp văn bản để trình diễn:
Python3
with
open
def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
0def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
1def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
2def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
3def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
4Phương pháp 1: & nbsp; Tìm chỉ mục của chuỗi trong tệp văn bản bằng readline []
Trong phương thức này, chúng tôi đang sử dụng hàm readline [] và kiểm tra hàm find [], phương thức này trả về -1 nếu không tìm thấy giá trị và nếu thấy nó trả về 0.
def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
5def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
6def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
7 def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
8def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
5with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
0 with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
1with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
2 with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
3with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
5with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
6with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
7with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
8with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
9string = "GEEK FOR GEEKS" Input: "FOR" Output: Yes, FOR is present in the given string.0
with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
6with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
7string = "GEEK FOR GEEKS" Input: "FOR" Output: Yes, FOR is present in the given string.3
string = "GEEK FOR GEEKS" Input: "FOR" Output: Yes, FOR is present in the given string.4
Output:
-1 -1 0 string exists in file line Number: 4 -1 -1
with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
4with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
5def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
7 with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
7
with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
4with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
9 with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
0def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
7def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
7 with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
3with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
4Python3
with
open
def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
0def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
1def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
2def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
3-1 -1 0 string exists in file line Number: 4 -1 -11
-1 -1 0 string exists in file line Number: 4 -1 -12
with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
4Phương pháp 2: Tìm chuỗi trong tệp văn bản bằng Read []
Chúng tôi sẽ tìm kiếm chuỗi từng dòng nếu chuỗi được tìm thấy thì chúng tôi sẽ in số chuỗi và số đó bằng hàm read [].
with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
5with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
6with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
7string does not exist7
with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
9with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
4string does not exist in a file0
with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
4with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
5with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
6with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
7string does not exist in a file5
with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
9Output:
string does not exist
with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
4-1
-1
0
string exists in file
line Number: 4
-1
-1
55def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
7 -1
-1
0
string exists in file
line Number: 4
-1
-1
2-1
-1
0
string exists in file
line Number: 4
-1
-1
8Search for a String in Text Files using enumerate[]
with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
4with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
9 string does not exist1
with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
2 string does not exist3
Python3
with
open
def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
0with
0def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
2def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
3with
3Phương pháp 3: Tìm kiếm một chuỗi trong các tệp văn bản bằng cách sử dụng Enumerate []
Chúng tôi chỉ tìm thấy chuỗi có mặt trong tệp hoặc không sử dụng Enumerate [] trong Python.
def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
5with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
0 with
6with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
2 with
8with
9with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
5def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
01def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
5with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
6with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
7def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
05with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
9Output:
string does not exist in a file