Trong hướng dẫn này, chúng ta sẽ tìm hiểu về phương thức thay thế[] trong Python với sự trợ giúp của các ví dụ
Phương thức thay thế [] thay thế mỗi lần xuất hiện khớp của một chuỗi con bằng một chuỗi khác
Các bài viết liên quan
- Theo dõi sự thay đổi google
- Tìm hiểu về mã hóa cổ điển
- Chuỗi Swift
- String in Golang
- Thay đổi kích thước hình ảnh OpenCV
Ví dụ
text = 'bat ball' # thay thế 'ba' với 'ro' replaced_text = text.replace['ba', 'ro'] print[replaced_text]
Cú pháp của nó là
str.replace[old, new [, count]]
Phương thức thay thế [] có thể nhận tối đa ba đối số
- old – string con old we want to replace
- chuỗi con mới sẽ thay thế chuỗi con cũ
- đếm[tùy chọn] – số lần bạn muốn thay thế chuỗi con cũ bằng chuỗi mới
Lưu ý. Nếu số đếm không được chỉ định, phương thức thay thế[] sẽ thay thế tất cả các lần xuất hiện của chuỗi con cũ bằng chuỗi mới
thay thế [] trở lại
Phương thức thay thế[] trả về một bản sao của chuỗi trong chuỗi con cũ đã được thay thế bằng chuỗi mới. Ban đầu chuỗi không thay đổi
Trong ví dụ sau, chúng ta sẽ thay thế chuỗi
#input file
fin = open["data.txt", "rt"]
#output file to write the result to
fout = open["out.txt", "wt"]
#for each line in the input file
for line in fin:
#read replace the string and write to output file
fout.write[line.replace['pyton', 'python']]
#close input and output files
fin.close[]
fout.close[]
6 bằng #input file
fin = open["data.txt", "rt"]
#output file to write the result to
fout = open["out.txt", "wt"]
#for each line in the input file
for line in fin:
#read replace the string and write to output file
fout.write[line.replace['pyton', 'python']]
#close input and output files
fin.close[]
fout.close[]
7 trong tệp #input file
fin = open["data.txt", "rt"]
#output file to write the result to
fout = open["out.txt", "wt"]
#for each line in the input file
for line in fin:
#read replace the string and write to output file
fout.write[line.replace['pyton', 'python']]
#close input and output files
fin.close[]
fout.close[]
8 và ghi kết quả vào #input file
fin = open["data.txt", "rt"]
#output file to write the result to
fout = open["out.txt", "wt"]
#for each line in the input file
for line in fin:
#read replace the string and write to output file
fout.write[line.replace['pyton', 'python']]
#close input and output files
fin.close[]
fout.close[]
9Chương trình Python
#input file
fin = open["data.txt", "rt"]
#output file to write the result to
fout = open["out.txt", "wt"]
#for each line in the input file
for line in fin:
#read replace the string and write to output file
fout.write[line.replace['pyton', 'python']]
#close input and output files
fin.close[]
fout.close[]
Chúng ta đã làm gì ở đây?
- Mở
8 ở chế độ đọc văn bản#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
1 và lấy tham chiếu đếnWelcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
2Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
- Mở
9 ở chế độ viết văn bản#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
4 và lấy tham chiếu đếnWelcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
5Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
6. cho mỗi dòng trongWelcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
2 i. e. ,Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
8,#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
9. thay thế chuỗiWelcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
6 bằng#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
7 và#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
str.replace[old, new [, count]]
02. viết thư cho
9#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
str.replace[old, new [, count]]
04. đóng tệp được tham chiếu bởi
2,Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
str.replace[old, new [, count]]
06. đóng tệp được tham chiếu bởi
5Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
Tệp đầu vào
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
Tệp đầu ra
str.replace[old, new [, count]]0
Chuỗi
#input file
fin = open["data.txt", "rt"]
#output file to write the result to
fout = open["out.txt", "wt"]
#for each line in the input file
for line in fin:
#read replace the string and write to output file
fout.write[line.replace['pyton', 'python']]
#close input and output files
fin.close[]
fout.close[]
6 trong tệp được thay thế bằng chuỗi #input file
fin = open["data.txt", "rt"]
#output file to write the result to
fout = open["out.txt", "wt"]
#for each line in the input file
for line in fin:
#read replace the string and write to output file
fout.write[line.replace['pyton', 'python']]
#close input and output files
fin.close[]
fout.close[]
7ví dụ 2. Thay thế chuỗi trong cùng một tệp
Trong ví dụ sau, chúng ta sẽ thay chuỗi python bằng python trong data. txt và ghi đè lên dữ liệu. txt với văn bản được thay thế
Chương trình Python
str.replace[old, new [, count]]7
Chúng ta đã làm gì ở đây?
- Mở tệp
8 ở chế độ đọc văn bản#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
1Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
str.replace[old, new [, count]]
72 đọc toàn bộ văn bản trong
8 đến biến#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
str.replace[old, new [, count]]
74str.replace[old, new [, count]]
75 thay thế tất cả các lần xuất hiện của
6 bằng#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
7 trong toàn bộ văn bản#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
str.replace[old, new [, count]]
04 đóng tệp đầu vào
8#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
- Trong ba dòng cuối cùng, chúng tôi đang mở
8 ở chế độ viết văn bản#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
4 và ghi dữ liệu vàoWelcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
8 ở chế độ thay thế. Cuối cùng đóng tệp#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
8#input file fin = open["data.txt", "rt"] #output file to write the result to fout = open["out.txt", "wt"] #for each line in the input file for line in fin: #read replace the string and write to output file fout.write[line.replace['pyton', 'python']] #close input and output files fin.close[] fout.close[]
Tệp đầu vào
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
Cùng một tệp đầu vào sau khi thực hiện chương trình
str.replace[old, new [, count]]0
Tóm lược
Trong hướng dẫn về Ví dụ Python này, chúng ta đã học cách thay thế một chuỗi bằng một chuỗi khác trong tệp, với sự trợ giúp của các ví dụ chi tiết