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
-1 -1 0 string exists in file line Number: 4 -1 -10
-1 -1 0 string exists in file line Number: 4 -1 -11
-1 -1 0 string exists in file line Number: 4 -1 -12
-1 -1 0 string exists in file line Number: 4 -1 -13
-1 -1 0 string exists in file line Number: 4 -1 -14
Phươ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.
-1 -1 0 string exists in file line Number: 4 -1 -15
-1 -1 0 string exists in file line Number: 4 -1 -16
-1 -1 0 string exists in file line Number: 4 -1 -17
-1 -1 0 string exists in file line Number: 4 -1 -18
-1 -1 0 string exists in file line Number: 4 -1 -15
string does not exist0
string does not exist1
string does not exist2
string does not exist3
string does not exist in a file5
string does not exist in a file6
string does not exist in a file7
string does not exist in a file8
string does not exist in a file9
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]]
0string does not exist in a file6
string does not exist in a file7
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]]
3def 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]]
4Output:
-1 -1 0 string exists in file line Number: 4 -1 -1
string does not exist
4string does not exist
5-1
-1
0
string exists in file
line Number: 4
-1
-1
7 string does not exist
7
string does not exist4
string does not exist9
string does not exist in a file0
-1 -1 0 string exists in file line Number: 4 -1 -17
-1 -1 0 string exists in file line Number: 4 -1 -17
string does not exist in a file3
string does not exist in a file4
Python3
with
open
-1 -1 0 string exists in file line Number: 4 -1 -10
-1 -1 0 string exists in file line Number: 4 -1 -11
-1 -1 0 string exists in file line Number: 4 -1 -12
-1 -1 0 string exists in file line Number: 4 -1 -13
def 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
2string does not exist in a file4
Phươ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 [].
string does not exist in a file5
string does not exist in a file6
string does not exist in a file7
with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
7string does not exist in a file9
string does not exist4
with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
0string does not exist in a file4
string does not exist in a file5
string does not exist in a file6
string does not exist in a file7
with open["file.txt", "r"] as fp:
for line in generate_lines_that_equal["my_string", fp]:
print line
5string does not exist in a file9
Output:
string does not exist
string does not exist
4def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
55-1
-1
0
string exists in file
line Number: 4
-1
-1
7 def 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
8Search for a String in Text Files using enumerate[]
def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
def generate_lines_that_equal[string, fp]:
for line in fp:
if line == string:
yield line
string does not exist4
string does not exist9
with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
1 string does not exist2
with open["file.txt", "r"] as fp:
for line in lines_that_equal["my_string", fp]:
print line
3Python3
with
open
-1 -1 0 string exists in file line Number: 4 -1 -10
with
0-1 -1 0 string exists in file line Number: 4 -1 -12
-1 -1 0 string exists in file line Number: 4 -1 -13
with
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.
-1 -1 0 string exists in file line Number: 4 -1 -15
string does not exist0
with
6string does not exist2
with
8with
9string does not exist in a file5
-1 -1 0 string exists in file line Number: 4 -1 -101
-1 -1 0 string exists in file line Number: 4 -1 -15
string does not exist in a file6
string does not exist in a file7
-1 -1 0 string exists in file line Number: 4 -1 -105
string does not exist in a file9
Output:
string does not exist in a file
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ớ.