Làm cách nào để thay thế một dòng trong tệp văn bản trong python?

Bài viết này đề cập đến một số chương trình trong Python thay thế một dòng cụ thể từ một tệp. Dưới đây là danh sách các chương trình được đề cập trong bài viết này

  • Thay thế dòng cụ thể trong một tệp
  • Thay thế dòng cụ thể trong tệp và in nội dung mới của tệp

Những việc cần làm trước Chương trình

Vì chương trình được cung cấp bên dưới hoạt động trên tệp, do đó phải có tệp trước khi thực hiện chương trình được cung cấp bên dưới. Do đó, hãy tạo một trình giải mã tên tệp. txt và đặt nội dung sau

This is a text file.
The name of this file is codescracker.txt
You are practicing Python by example here

bên trong tập tin đó. Lưu tệp bên trong thư mục hiện tại. Ở đây thư mục hiện tại có nghĩa là thư mục lưu mã nguồn của chương trình. Đây là ảnh chụp nhanh của tệp được lưu trữ trong thư mục hiện tại

Làm cách nào để thay thế một dòng trong tệp văn bản trong python?

Ví dụ. Thay thế Văn bản bằng Mô-đun Regex

Một phương pháp thay thế cho các phương pháp nêu trên là sử dụng mô-đun

reading_file = open("review.txt", "r")

new_file_content = ""
for line in reading_file:
    stripped_line = line.strip()
    new_line = stripped_line.replace("Ghost", "Ghostbusters")
    new_file_content += new_line +"\n"
reading_file.close()

writing_file = open("review.txt", "w")
writing_file.write(new_file_content)
writing_file.close()
1 của Python. Ví dụ dưới đây nhập mô-đun regex. Nó tạo một hàm và chuyển một tệp, một chuỗi cũ và một chuỗi mới làm đối số. Bên trong hàm ta mở file ở cả 2 chế độ đọc ghi và đọc nội dung của file

reading_file = open("review.txt", "r")

new_file_content = ""
for line in reading_file:
    stripped_line = line.strip()
    new_line = stripped_line.replace("Ghost", "Ghostbusters")
    new_file_content += new_line +"\n"
reading_file.close()

writing_file = open("review.txt", "w")
writing_file.write(new_file_content)
writing_file.close()
2 - Nó được sử dụng để biên dịch một mẫu biểu thức chính quy và chuyển đổi nó thành một đối tượng biểu thức chính quy mà sau đó có thể được sử dụng để so khớp

reading_file = open("review.txt", "r")

new_file_content = ""
for line in reading_file:
    stripped_line = line.strip()
    new_line = stripped_line.replace("Ghost", "Ghostbusters")
    new_file_content += new_line +"\n"
reading_file.close()

writing_file = open("review.txt", "w")
writing_file.write(new_file_content)
writing_file.close()
3 - Nó được sử dụng để thoát các ký tự đặc biệt trong một mẫu

reading_file = open("review.txt", "r")

new_file_content = ""
for line in reading_file:
    stripped_line = line.strip()
    new_line = stripped_line.replace("Ghost", "Ghostbusters")
    new_file_content += new_line +"\n"
reading_file.close()

writing_file = open("review.txt", "w")
writing_file.write(new_file_content)
writing_file.close()
4 - Nó được sử dụng để thay thế một mẫu bằng một chuỗi

print("Enter the name of file: ")
filename = input()

filehandle = open(filename, "r")
listOfLines = filehandle.readlines()
filehandle.close()

print("Enter line number to replace for: ")
lineNo = int(input())
print("Enter new content for line number", lineNo, ": ")
newline = input()
listOfLines[lineNo] = newline

filehandle = open(filename, "w")
filehandle.writelines(listOfLines)
filehandle.close()

print("\nLine replaced successfully!")
0

đầu ra

Làm cách nào để thay thế một dòng trong tệp văn bản trong python?

Đầu vào tệp bằng Python

reading_file = open("review.txt", "r")

new_file_content = ""
for line in reading_file:
    stripped_line = line.strip()
    new_line = stripped_line.replace("Ghost", "Ghostbusters")
    new_file_content += new_line +"\n"
reading_file.close()

writing_file = open("review.txt", "w")
writing_file.write(new_file_content)
writing_file.close()
5 là một tính năng hữu ích của Python để thực hiện các hoạt động khác nhau liên quan đến tệp. Để sử dụng FileInput, mô-đun
reading_file = open("review.txt", "r")

new_file_content = ""
for line in reading_file:
    stripped_line = line.strip()
    new_line = stripped_line.replace("Ghost", "Ghostbusters")
    new_file_content += new_line +"\n"
reading_file.close()

writing_file = open("review.txt", "w")
writing_file.write(new_file_content)
writing_file.close()
6 được nhập. Nó là tuyệt vời cho các kịch bản vứt đi. Nó cũng được sử dụng để thay thế nội dung trong một tập tin. Nó thực hiện tìm kiếm, chỉnh sửa và thay thế trong một tệp văn bản. Nó không tạo bất kỳ tệp mới hoặc chi phí nào

Cú pháp-

print("Enter the name of file: ")
filename = input()

filehandle = open(filename, "r")
listOfLines = filehandle.readlines()
filehandle.close()

print("Enter line number to replace for: ")
lineNo = int(input())
print("Enter new content for line number", lineNo, ": ")
newline = input()
listOfLines[lineNo] = newline

filehandle = open(filename, "w")
filehandle.writelines(listOfLines)
filehandle.close()

print("\nLine replaced successfully!")
3

Thông số-

reading_file = open("review.txt", "r")

new_file_content = ""
for line in reading_file:
    stripped_line = line.strip()
    new_line = stripped_line.replace("Ghost", "Ghostbusters")
    new_file_content += new_line +"\n"
reading_file.close()

writing_file = open("review.txt", "w")
writing_file.write(new_file_content)
writing_file.close()
7 - Bản sao lưu là phần mở rộng cho tệp sao lưu được tạo trước khi chỉnh sửa

Ví dụ. Tìm kiếm và thay thế văn bản bằng hàm FileInput và replace()

Hàm dưới đây thay thế một văn bản bằng hàm

print("Enter the name of file: ")
filename = input()

filehandle = open(filename, "r")
listOfLines = filehandle.readlines()
filehandle.close()

print("Enter line number to replace for: ")
lineNo = int(input())
print("Enter new content for line number", lineNo, ": ")
newline = input()
listOfLines[lineNo] = newline

filehandle = open(filename, "w")
filehandle.writelines(listOfLines)
filehandle.close()

print("\nLine replaced successfully!")
8

print("Enter the name of file: ")
filename = input()

filehandle = open(filename, "r")
listOfLines = filehandle.readlines()
filehandle.close()

print("Enter line number to replace for: ")
lineNo = int(input())
print("Enter new content for line number", lineNo, ": ")
newline = input()
listOfLines[lineNo] = newline

filehandle = open(filename, "w")
filehandle.writelines(listOfLines)
filehandle.close()

print("\nLine replaced successfully!")
5

đầu ra

Làm cách nào để thay thế một dòng trong tệp văn bản trong python?

Phần kết luận

Trong bài viết này, chúng ta đã học cách tìm kiếm và thay thế một văn bản hoặc một chuỗi trong tệp bằng cách sử dụng một số hàm có sẵn như mô-đun

print("Enter the name of file: ")
filename = input()

filehandle = open(filename, "r")
listOfLines = filehandle.readlines()
filehandle.close()

print("Enter line number to replace for: ")
lineNo = int(input())
print("Enter new content for line number", lineNo, ": ")
newline = input()
listOfLines[lineNo] = newline

filehandle = open(filename, "w")
filehandle.writelines(listOfLines)
filehandle.close()

print("\nLine replaced successfully!")
8,
reading_file = open("review.txt", "r")

new_file_content = ""
for line in reading_file:
    stripped_line = line.strip()
    new_line = stripped_line.replace("Ghost", "Ghostbusters")
    new_file_content += new_line +"\n"
reading_file.close()

writing_file = open("review.txt", "w")
writing_file.write(new_file_content)
writing_file.close()
1 và
reading_file = open("review.txt", "r")

new_file_content = ""
for line in reading_file:
    stripped_line = line.strip()
    new_line = stripped_line.replace("Ghost", "Ghostbusters")
    new_file_content += new_line +"\n"
reading_file.close()

writing_file = open("review.txt", "w")
writing_file.write(new_file_content)
writing_file.close()
5. Chúng tôi cũng đã sử dụng một số mã tùy chỉnh. Chúng tôi cũng đã thấy kết quả đầu ra để phân biệt giữa các ví dụ. Do đó, để tìm kiếm và thay thế một chuỗi trong Python, người dùng có thể tải toàn bộ tệp và sau đó thay thế nội dung trong cùng một tệp thay vì tạo một tệp mới rồi ghi đè lên tệp