Nội dung Nội phân Chính showShow
- 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
- Bản tóm tắt
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
- Bản tóm tắt
Để thay thế một chuỗi trong tệp bằng Python, hãy làm theo các bước sau:
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
Mở tệp đầu vào ở chế độ đọc và xử lý nó ở chế độ văn bản.
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
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.
Phương pháp 2: Sử dụng chức năng thay thế trong vòng lặp
#read input file
fin = open["data.txt", "rt"]
#read file contents to string
data = fin.read[]
#replace all occurrences of the required string
data = data.replace['pyton', 'python']
#close the input file
fin.close[]
#open the input file in write mode
fin = open["data.txt", "wt"]
#overrite the input file with the resulting data
fin.write[data]
#close the file
fin.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
- Bản tóm tắt
- Để thay thế một chuỗi trong tệp bằng Python, hãy làm theo các bước sau:
Để thay thế một chuỗi trong tệp bằng Python, hãy làm theo các bước sau:
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
Mở tệp đầu vào ở chế độ đọc và xử lý nó ở chế độ văn bản.
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.
Đố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.
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
Trong ví dụ sau, chúng tôi sẽ thay thế chuỗi
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 Python
Chúng ta đã làm gì ở đây?
Enter text to replace the existing contents: Geeks Text successfully replaced2
Enter text to replace the existing contents: Geeks Text successfully replaced3
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3Enter text to replace the existing contents: Geeks Text successfully replaced5
Enter text to replace the existing contents: Geeks Text successfully replaced6
Enter text to replace the existing contents: Geeks Text successfully replaced7
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1Enter text to replace the existing contents: Geeks Text successfully replaced9
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3Output:
Enter text to replace the existing contents: Geeks Text successfully replaced
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
Chúng ta đã làm gì ở đây?
Mở
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
2 trong Chế độ đọc văn bản Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
5 và nhận tham chiếu đến Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
6.Mở
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
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.pythonexamples.org. Here, you will find python programs for all general use cases.
0: Đối với mỗi dòng trong Welcome to www.pytonexamples.org. Here, you will find pyton 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.pythonexamples.org. Here, you will find python programs for all general use cases.
3: Thay thế chuỗi 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 và Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
6: ghi vào Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
3.Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
8: Đó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.
6, #read input file
fin = open["data.txt", "rt"]
#read file contents to string
data = fin.read[]
#replace all occurrences of the required string
data = data.replace['pyton', 'python']
#close the input file
fin.close[]
#open the input file in write mode
fin = open["data.txt", "wt"]
#overrite the input file with the resulting data
fin.write[data]
#close the file
fin.close[]
0: Đó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.
9.Tệp đầu vào
Tệp đầu ra
Chuỗi
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
0 trong tệp được thay thế bằng chuỗi Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
1.Enter text to replace the existing contents: Geeks Text successfully replaced2
Enter text to replace the existing contents: Geeks Text successfully replaced3
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
22Enter text to replace the existing contents: Geeks Text successfully replaced6
Enter text to replace the existing contents: Geeks Text successfully replaced7
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1Enter text to replace the existing contents: Geeks Text successfully replaced9
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3Output:
Enter text to be replaced: Geeks Enter text that will replace: Geekforgeeks Text successfully replaced
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
Bản tóm tắt
Để thay thế một chuỗi trong tệp bằng Python, hãy làm theo các bước sau:
Chúng ta đã làm gì ở đây?
Mở
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
2 trong Chế độ đọc văn bản Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
5 và nhận tham chiếu đến Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
6.Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
52Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
53Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
8Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
56Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
48Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
9Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
8Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
61Enter text to replace the existing contents: Geeks Text successfully replaced7
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
64Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3Output:
Enter text that will replace the existing text: geeks File replaced
Phương pháp 4: Sử dụng FileInput.Input []
Phương thức fileInput.Input [] nhận tệp làm dòng đầu vào từng dòng và chủ yếu được sử dụng để nối thêm và cập nhật dữ liệu trong tệp đã cho.Mô -đun FileInput và SYS cần được nhập vào mã Python hiện tại để chạy mã mà không có bất kỳ lỗi nào.Mã sau sử dụng hàm FileInput.Input [] để thay thế văn bản trong một dòng trong Python.
Python3
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
28 Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
67Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
28 Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
69Enter text to be replaced: Geeks Enter text that will replace: Geekforgeeks Text successfully replaced1
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
9 Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
0Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
74Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3Enter text to be replaced: Geeks Enter text that will replace: Geekforgeeks Text successfully replaced7
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
9 Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
0Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
80Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks7
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks1
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks9
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
85Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
0__Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
01Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks1
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.
94Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
01Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
96Output:
Enter text to be replaced: Geeks Enter replacement text: Geeksforgeeks