Hướng dẫn python open replace - python mở thay thế

Nội dung

Nội phân Chính showShow

  • 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

  1. Phương pháp 3: Sử dụng mô -đun HĐH để thay thế tệp bằng văn bản mới
  2. Phương pháp 4: Sử dụng FileInput.Input []
  3. Giới thiệu
  4. 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

  1. Phương pháp 3: Sử dụng mô -đun HĐH để thay thế tệp bằng văn bản mới
  2. Phương pháp 4: Sử dụng FileInput.Input []
  3. Giới thiệu
  4. 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

  1. Phương pháp 3: Sử dụng mô -đun HĐH để thay thế tệp bằng văn bản mới
  2. Phương pháp 4: Sử dụng FileInput.Input []
  3. Giới thiệu
  4. Bản tóm tắt
  5. Để 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 replaced
2
Enter text to replace the existing contents: Geeks
Text successfully replaced
3
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3

Enter text to replace the existing contents: Geeks
Text successfully replaced
5

Enter text to replace the existing contents: Geeks
Text successfully replaced
6

Enter text to replace the existing contents: Geeks
Text successfully replaced
7
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1
Enter text to replace the existing contents: Geeks
Text successfully replaced
9
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3

Output:  

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 replaced
2
Enter text to replace the existing contents: Geeks
Text successfully replaced
3
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.
22

Enter text to replace the existing contents: Geeks
Text successfully replaced
6

Enter text to replace the existing contents: Geeks
Text successfully replaced
7
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1
Enter text to replace the existing contents: Geeks
Text successfully replaced
9
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3

Output:

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.
52

Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
53
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
8
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.
56
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
48
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
9
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
8
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.
61

Enter text to replace the existing contents: Geeks
Text successfully replaced
7
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
64
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3

Output:

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.
67

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.
69

Enter text to be replaced: Geeks
Enter text that will replace: Geekforgeeks
Text successfully replaced
1
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
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
74
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3

Enter text to be replaced: Geeks
Enter text that will replace: Geekforgeeks
Text successfully replaced
7
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
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
1
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
80
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3

Enter text to be replaced: Geeks
Enter replacement text: Geeksforgeeks
7
Enter text to be replaced: Geeks
Enter replacement text: Geeksforgeeks
1
Enter text to be replaced: Geeks
Enter replacement text: Geeksforgeeks
9
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
85
Welcome 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.
01
Enter text to be replaced: Geeks
Enter replacement text: Geeksforgeeks
1
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.
94

Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
01
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
96

Output:

Enter text to be replaced: Geeks
Enter replacement text: Geeksforgeeks

Chủ Đề