Thay thế tập tin python

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[]
9

Chươ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?

  1. Mở
    #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 ở chế độ đọc văn bản
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    1 và lấy tham chiếu đến
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    2
  2. Mở
    #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 ở chế độ viết văn bản
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    4 và lấy tham chiếu đến
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    5
  3. Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    6. cho mỗi dòng trong
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    2 i. e. ,
    #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,
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    9. 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 và
    str.replace[old, new [, count]] 
    02. viết thư cho
    #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
  4. str.replace[old, new [, count]] 
    04. đóng tệp được tham chiếu bởi
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    2,
    str.replace[old, new [, count]] 
    06. đóng tệp được tham chiếu bởi
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    5

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[]
7

ví 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?

  1. Mở 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 ở chế độ đọc văn bản
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    1
  2. str.replace[old, new [, count]] 
    72 đọc toàn bộ văn bản trong
    #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 đến biến
    str.replace[old, new [, count]] 
    74
  3. str.replace[old, new [, count]] 
    75 thay thế tất cả các lần xuất hiện của
    #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 toàn bộ văn bản
  4. str.replace[old, new [, count]] 
    04 đóng tệp đầu 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[]
    8
  5. Trong ba dòng cuối cùng, chúng tôi đang mở
    #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 ở chế độ viết văn bản
    Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
    4 và ghi dữ liệu 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[]
    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

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

Chủ Đề