Làm thế nào để bạn thay đổi một chuỗi thành một tệp trong python?

Xử lý tệp là một khía cạnh thiết yếu của bất kỳ ứng dụng web nào. Python, tương tự như các ngôn ngữ lập trình khác, hỗ trợ xử lý tệp. Nó cho phép các lập trình viên xử lý tệp và về cơ bản thực hiện một số thao tác cơ bản như đọc, viết và một số tùy chọn xử lý tệp khác để thao tác trên tệp

Hàm

with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
2 có thể được sử dụng để mở tệp trong chương trình Python. Tệp có thể được mở ở chế độ văn bản hoặc chế độ nhị phân do người dùng quyết định. Hàm
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
2 có một số chế độ, tất cả đều cung cấp các tùy chọn trợ năng khác nhau để mở tệp trong

Thuật ngữ chuỗi trong Python có thể được mô tả dưới dạng một cụm ký tự Unicode được đặt trong dấu ngoặc đơn hoặc dấu ngoặc kép. Các chuỗi có thể được chứa bên trong các tệp văn bản sẽ được mở bằng mã Python

Hướng dẫn này sẽ thảo luận về các phương pháp khác nhau để thay thế một chuỗi trong tệp bằng Python

Sử dụng hàm
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
4 khi các tệp đầu vào và đầu ra khác nhau

Phương thức

with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
4 trong Python được sử dụng để tìm kiếm một chuỗi con và thay thế nó bằng một chuỗi con khác

Hàm

with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
4 có ba tham số là
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
7,______18 và
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
9. Cả
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
7 và
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
8 đều là các giá trị bắt buộc và việc cung cấp hàm với tham số
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
9 là tùy chọn

Đoạn mã sau sử dụng hàm

with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
4 để thay thế một chuỗi trong Python khi các tệp đầu vào và đầu ra khác nhau

# the input file
fin = open["f1.txt", "rt"]
# the output file which stores result
fout = open["f2.txt", "wt"]
# iteration for each line in the input file
for line in fin:
	# replacing the string and write to output file
	fout.write[line.replace['gode', 'God']]
#closing the input and output files
fin.close[]
fout.close[]

Ở đầu ra của đoạn mã trên, chuỗi

with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
4 trong tệp sẽ được thay thế bằng từ
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
5

Trong đoạn mã trên, chúng tôi đồng thời làm việc trên hai tệp khác nhau,

with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
6 và
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
7.
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
6 được mở ở chế độ đọc văn bản
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
9 và được tham chiếu đến
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
20.
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
7 được mở ở chế độ viết văn bản
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
22 và được tham chiếu đến
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
23. Sau đó, vòng lặp
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
24 được lặp lại và cứ mỗi lần xuất hiện của chuỗi
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
4 trong tệp, nó sẽ được thay thế bằng từ
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
5. Cả hai tệp sau đó được đóng lại sau các thao tác cần thiết với sự trợ giúp của hàm
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
27

Sử dụng hàm
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
4 khi chỉ sử dụng một tệp cho đầu vào và đầu ra

Trong phương pháp này, cùng một tệp được sử dụng làm cả đầu vào và đầu ra

Chúng tôi sử dụng câu lệnh

with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
29 ở đây cùng với hàm
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
4. Trình quản lý ngữ cảnh
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
29 có chức năng cơ bản. làm cho chương trình ngắn hơn và dễ đọc hơn nhiều

Khi chúng tôi sử dụng câu lệnh

with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
29 với Xử lý tệp, tệp mà chúng tôi đã mở bằng mã Python không cần phải đóng theo cách thủ công;

Đoạn mã sau sử dụng hàm

with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
4 để thay thế một chuỗi trong Python khi tệp đầu vào và tệp đầu ra giống nhau

with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]

Đoạn mã sau lấy

with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
25 làm cả tệp đầu vào và tệp đầu ra. Đầu tiên, tệp được mở ở chế độ đọc văn bản
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
9 và nội dung của tệp được đọc và lưu trữ trong một biến. Sau đó, tệp được đóng và mở lại, nhưng lần này ở chế độ ghi văn bản
with open["file1.txt", "rt"] as file:
	x = file.read[]
	
with open["file1.txt", "wt"] as file:
    x = x.replace["gode","God"]
	fin.write[x]
22. Chuỗi được thay thế và dữ liệu được ghi ở chế độ này và sau đó đóng tệp

Chủ Đề