Hàm replace[] trong Python trả về một bản sao của chuỗi ban đầu sau khi đã thay thế các chuỗi con cũ bằng chuỗi con mới.replace[] trong Python trả về một bản sao của chuỗi ban đầu sau khi đã thay thế các chuỗi con cũ bằng chuỗi con mới. Cú pháp của replace[] trong Python:replace[] trong Python:Cú pháp
str.replace[old, new[, max]]
Các tham số:
old: Đây là chuỗi con cũ để được thay thế.: Đây là chuỗi con cũ để được thay thế.
new: Đây là chuỗi con mới để thay thế cho chuỗi con cũ.: Đây là chuỗi con mới để thay thế cho chuỗi con cũ.
max: Nếu tham số tùy ý max này được cung cấp, thì chỉ có các sự xuất hiện đầu tiên được thay thế.: Nếu tham số tùy ý max này được cung cấp, thì chỉ có các sự xuất hiện đầu tiên được thay thế.
Ví dụ sau minh họa cách sử dụng của hàm replace[] trong Python.
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]
Chạy chương trình Python trên sẽ cho kết quả:
Vi du ham replace[] Python tren VietTuts.Vn Vi du phuong thuc replace[] Python
Để thay thế một chuỗi trong tệp bằng Python, hãy làm theo các bước sau:
Mở tệp đầu vào ở chế độ đọc và xử lý nó ở chế độ văn bản.
E:\p\folder\folder2\sw\first_step\step1\step2\final
E:\p\folder\folder2\sw\second_step\step1\step2\final
E:\p\folder\folder2\sw\second_step\step56\step333\final
E:\p\folder\folder2\sw\second_step\step1\step2\final
etc
Mở tệp đầu ra ở chế độ ghi và xử lý nó ở chế độ văn bản.
def findReplace[directory, find, replace, filePattern]:
for path, dirs, files in os.walk[os.path.abspath[directory]]:
for filename in fnmatch.filter[files, filePattern]:
filepath = os.path.join[path, filename]
with open[filepath] as f:
s = f.read[]
s = s.replace[find, replace]
with open[filepath, "w"] as f:
f.write[s]
base_dir = 'F:/Users/User1/Folder/'
path_to_files = '/folder1/folder2/files/'
directory = os.listdir[os.path.join[base_dir, path_to_includes_appl]]
for file in directory:
if file == None: continue
with open [file, 'r'] as f:
content = f.read[].split['\n']
for line in content:
if not line: continue
# Idea here is to sepeare part we need to replace with part that has to stay
old_path, path_to_file = line.split['sw\\']
findReplace[str[directory], old_path, base_dir, ".c"]
Đối với mỗi dòng đọc từ tệp đầu vào, thay thế chuỗi và ghi vào tệp đầu ra.
F:\Users\User1\Folder\sw\first_step\step1\step2\final
F:\Users\User1\Folder\second_step\step1\step2\final
F:\Users\User1\Folder\second_step\step56\step333\final
F:\Users\User1\Folder\second_step\step1\step2\final
Đóng cả tệp đầu vào và đầu ra.
Phương pháp 1: Xóa tất cả văn bản và viết văn bản mới trong cùng một tệp
EDIT:
Trong ví dụ sau, chúng tôi sẽ thay thế chuỗi
base_dir = 'F:/Users/User1/Folder/'
path_to_includes_appl = '/folder1/folder2/files/'
# directory = os.listdir[os.path.join[base_dir, path_to_includes_appl]]
directory_path = [os.path.join[base_dir, path_to_includes_appl]]
directory = os.listdir[directory_path]
for file in directory:
with open [directory_path+file, 'r'] as f:
content = f.readlines[]
for line in content:
old_path, path_to_file = line.split['sw\\']
findReplace[directory_path, old_path, base_dir, ".c"]
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
0 bằng Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
1 trong tệp Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
2 và viết kết quả thành Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
3.
3 trong chế độ viết văn bản Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
8 và nhận tham chiếu đến Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
9.Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
0: Đối với mỗi dòng trong Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6, tức là, Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
2, Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
3: Thay thế chuỗi Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
0 bằng Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
1 và Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
6: ghi vào Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3.Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
8: Đóng tệp được tham chiếu bởi Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6, Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
str1 = "Vi du ham replace[] Python"
print [str1.replace["Python", "Python tren VietTuts.Vn"]]
print [str1.replace["ham", "phuong thuc", 1]]
00: Đóng tệp được tham chiếu bởi
9.Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
Tệp đầu vàoShow
- Ví dụ 1: Thay thế chuỗi trong tệp
- Ví dụ 2: Thay thế chuỗi trong cùng một tệp
- Mở tệp đầu ra ở chế độ ghi và xử lý nó ở chế độ văn bản.
- Phương pháp 1: Xóa tất cả văn bản và viết văn bản mới trong cùng một tệp
- Phương pháp 2: Sử dụng chức năng thay thế trong vòng lặp
- Phương pháp 3: Sử dụng mô -đun HĐH để thay thế tệp bằng văn bản mới
- Giới thiệu
- Phương pháp 4: Sử dụng FileInput.Input []
Nội phân chính
- Ví dụ 1: Thay thế chuỗi trong tệp
- Ví dụ 2: Thay thế chuỗi trong cùng một tệp
- Phương pháp 1: Xóa tất cả văn bản và viết văn bản mới trong cùng một tệp
- Phương pháp 2: Sử dụng chức năng thay thế trong vòng lặp
- Phương pháp 3: Sử dụng mô -đun HĐH để thay thế tệp bằng văn bản mới
- Phương pháp 4: Sử dụng FileInput.Input []
- Giới thiệu
- Ví dụ 1: Thay thế chuỗi trong tệp
- Ví dụ 2: Thay thế chuỗi trong cùng một tệp
- Phương pháp 1: Xóa tất cả văn bản và viết văn bản mới trong cùng một tệp
Phương pháp 2: Sử dụng chức năng thay thế trong vòng lặp
- Phương pháp 3: Sử dụng mô -đun HĐH để thay thế tệp bằng văn bản mới
- Phương pháp 4: Sử dụng FileInput.Input []
- Giới thiệu
- Phương pháp 4: Sử dụng FileInput.Input []
Ví dụ 1: Thay thế chuỗi trong tệp
Ví dụ 2: Thay thế chuỗi trong cùng một tệp
Phương pháp 1: Xóa tất cả văn bản và viết văn bản mới trong cùng một 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[]
Phương pháp 2: Sử dụng chức năng thay thế trong vòng lặp
- Phương pháp 3: Sử dụng mô -đun HĐH để thay thế tệp bằng văn bản mới
- Phương pháp 4: Sử dụng FileInput.Input []
- Giới thiệu
- Phương pháp 4: Sử dụng FileInput.Input []
Nội phân chính
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
Ví dụ 1: Thay thế chuỗi trong tệp
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
Mở tệp đầu ra ở chế độ ghi và xử lý nó ở chế độ văn bản.
Ví dụ 2: Thay thế chuỗi trong cùng một tệp
Bản tóm tắtpyton with python in data.txt file, and overwrite the data.txt file with the replaced text.
Phương pháp 2: Sử dụng chức năng thay thế trong vòng lặp
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]0
Phương pháp 2: Sử dụng chức năng thay thế trong vòng lặp
- Phương pháp 3: Sử dụng mô -đun HĐH để thay thế tệp bằng văn bản mới
- Phương pháp 4: Sử dụng FileInput.Input []
- Giới thiệu
- Phương pháp 4: Sử dụng FileInput.Input []
- Nội phân chính
Nội phân chính
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
Ví dụ 1: Thay thế chuỗi trong tệp
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
Mở tệp đầu ra ở chế độ ghi và xử lý nó ở chế độ văn bản.
Ví dụ 2: Thay thế chuỗi trong cùng một tệp
Bản tóm tắt
Phương pháp 1: Xóa tất cả văn bản và viết văn bản mới trong cùng một tệp
Để thay thế một chuỗi trong tệp bằng Python, hãy làm theo các bước sau:
Mở tệp đầu vào ở chế độ đọc và xử lý nó ở chế độ văn bản.Python3
Phương pháp 1: Xóa tất cả văn bản và viết văn bản mới trong cùng một tệppyton with python in data.txt file, and overwrite the data.txt file with the replaced text.
Đối với mỗi dòng đọc từ tệp đầu vào, thay thế chuỗi và ghi vào tệp đầu ra.
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]72
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]73
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]75
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]76
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]77
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]79
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3Đóng cả tệp đầu vào và đầu ra.
str1 = "Vi du ham replace[] Python" print [str1.replace["Python", "Python tren VietTuts.Vn"]] print [str1.replace["ham", "phuong thuc", 1]]7
Phương pháp 2: Sử dụng chức năng thay thế trong vòng lặp
Phương pháp 3: Sử dụng mô -đun HĐH để thay thế tệp bằng văn bản mới
Python3
Phương pháp 4: Sử dụng FileInput.Input []
Giới thiệu
Đối với mỗi dòng đọc từ tệp đầu vào, thay thế chuỗi và ghi vào tệp đầu ra.
Đóng cả tệp đầu vào và đầu ra.
Trong ví dụ sau, chúng tôi sẽ thay thế chuỗiĐóng cả tệp đầu vào và đầu ra.
Trong ví dụ sau, chúng tôi sẽ thay thế chuỗiWelcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
0 bằng Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
1 trong tệp Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
2 và viết kết quả thành Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
3.Chương trình Python
Chúng ta đã làm gì ở đây?
Output:Output:
F:\Users\User1\Folder\sw\first_step\step1\step2\final
F:\Users\User1\Folder\second_step\step1\step2\final
F:\Users\User1\Folder\second_step\step56\step333\final
F:\Users\User1\Folder\second_step\step1\step2\final
6Phương pháp 3: Sử dụng mô -đun HĐH để thay thế tệp bằng văn bản mới
Phương pháp 4: Sử dụng FileInput.Input []
Giới thiệu
Phương pháp 4: Sử dụng FileInput.Input []
Nội phân chính
Đối với mỗi dòng đọc từ tệp đầu vào, thay thế chuỗi và ghi vào tệp đầu ra.
Đóng cả tệp đầu vào và đầu ra.
Trong ví dụ sau, chúng tôi sẽ thay thế chuỗiOutput:
#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[]
4Phương pháp 4: Sử dụng FileInput.Input []
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
0 bằng Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
1 trong tệp Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
2 và viết kết quả thành Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
3.Python3
Chương trình PythonOutput:
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1