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
MemTotal,5,2016-07-30 12:02:33,781
model name,3,2016-07-30 13:37:59,074
model,3,2016-07-30 15:39:59,075
0string does not exist in a file6
string does not exist in a file7
MemTotal,5,2016-07-30 12:02:33,781
model name,3,2016-07-30 13:37:59,074
model,3,2016-07-30 15:39:59,075
3MemTotal,5,2016-07-30 12:02:33,781
model name,3,2016-07-30 13:37:59,074
model,3,2016-07-30 15:39:59,075
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
term = "model"
file = open['file.txt']
for line in file:
line.strip[].split['/n']
if term in line:
print line
file.close[]
1term = "model"
file = open['file.txt']
for line in file:
line.strip[].split['/n']
if term in line:
print line
file.close[]
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
model name,3,2016-07-30 13:37:59,074
model,3,2016-07-30 15:39:59,075
7string does not exist in a file9
string does not exist4
model,3,2016-07-30 15:39:59,075
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
model,3,2016-07-30 15:39:59,075
5string does not exist in a file9
Output:
string does not exist
string does not exist
4term = "model"
file = open['file.txt']
for line in file:
line.strip[].split['/n']
if term in line:
print line
file.close[]
55-1
-1
0
string exists in file
line Number: 4
-1
-1
7 term = "model"
file = open['file.txt']
for line in file:
line.strip[].split['/n']
if term in line:
print line
file.close[]
2term = "model"
file = open['file.txt']
for line in file:
line.strip[].split['/n']
if term in line:
print line
file.close[]
8Search for a String in Text Files using enumerate[]
term = "model"
file = open['file.txt']
for line in file:
line.strip[].split['/n']
if term in line:
print line
file.close[]
term = "model"
file = open['file.txt']
for line in file:
line.strip[].split['/n']
if term in line:
print line
file.close[]
term = "model"
file = open['file.txt']
for line in file:
line.strip[].split['/n']
if term in line:
print line
file.close[]
string does not exist4
string does not exist9
model name,3,2016-07-30 13:37:59,074
model,3,2016-07-30 15:39:59,075
1 string does not exist2
model name,3,2016-07-30 13:37:59,074
model,3,2016-07-30 15:39:59,075
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
Tôi có tệp văn bản này:
MemTotal,5,2016-07-30 12:02:33,781
model name,3,2016-07-30 13:37:59,074
model,3,2016-07-30 15:39:59,075
Tôi cần tìm dòng với mô hình.
Mã của tôi:
term = "model"
file = open['file.txt']
for line in file:
line.strip[].split['/n']
if term in line:
print line
file.close[]
Đây là đầu ra:
model name,3,2016-07-30 13:37:59,074
model,3,2016-07-30 15:39:59,075
Tôi chỉ cần dòng này làm đầu ra:
model,3,2016-07-30 15:39:59,075
Tôi có thể làm cái này như thế nào?