Cách ghi vào một dòng cụ thể 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 Show
Những việc cần làm trước Chương trìnhVì 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 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 ['Fifth line', 'Eighth Line'] mô-đun linecache Đọc dòng từ tệp theo số dòngTrong phần này, chúng ta sẽ xem cách đọc tệp theo số dòng bằng mô-đun linecache Linecache của Python là một cách khác được tối ưu hóa hiệu suất để chuyển đến một dòng cụ thể trong tệp văn bản. Giả sử nếu bạn có một tệp văn bản lớn, thì linecache là lựa chọn chính xác
Sử dụng phương 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!")20 để đọc dòng cụ thể từ một tệ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!")2
Ví dụ 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!")7 đầu ra 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 Ghi chú. Linache đọc toàn bộ tệp trong bộ nhớ. Vì vậy, nếu truy cập ngẫu nhiên vào số dòng quan trọng hơn hiệu suất, thì hãy sử dụng linache Nếu bạn muốn đọc nhiều hơn một số dòng từ một tệp bằng linecache, hãy sử dụng ví dụ bên dướ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!")9 Sử dụng 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!")24 để đọc phạm vi dòng từ TệpNếu kích thước tệp của bạn nhỏ và bạn không quan tâm đến hiệu suất, thì phương thức readlines() là phù hợp nhất Đọc tệp bằng Python nhanh nếu kích thước tệp tính bằng vài MB Phương thức 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!")24 đọc tất cả các dòng từ một tệp và lưu trữ nó trong một danh sách. Bạn có thể sử dụng số chỉ mục làm số dòng để trích xuất một tập hợp các dòng từ nó Đây là cách đơn giản nhất để đọc một dòng cụ thể từ một tệp trong Python. Chúng tôi đọc toàn bộ tệp bằng cách này và sau đó chọn các dòng cụ thể từ tệp theo yêu cầu của chúng tôi Sử dụng 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!")26 để đọc phạm vi dòng
Ví dụ. Đọc dòng từ 3 đến 5 print("Enter File's Name: ", end="") fname = input() fhandle = open(fname, "r") lines = fhandle.readlines() fhandle.close() print("Enter line number to replace it: ", end="") lno = int(input()) print("Enter new content for line no.", lno, "\b: ", end="") nline = input() print("\n------Old Content-------") content = "" content = content.join(lines) print(content) lines[lno] = nline fhandle = open(fname, "w") fhandle.writelines(lines) fhandle.close() print("\n-----New Content--------") content = "" content = content.join(lines) print(content)5 Ví dụ. Đọc dòng 8 print("Enter File's Name: ", end="") fname = input() fhandle = open(fname, "r") lines = fhandle.readlines() fhandle.close() print("Enter line number to replace it: ", end="") lno = int(input()) print("Enter new content for line no.", lno, "\b: ", end="") nline = input() print("\n------Old Content-------") content = "" content = content.join(lines) print(content) lines[lno] = nline fhandle = open(fname, "w") fhandle.writelines(lines) fhandle.close() print("\n-----New Content--------") content = "" content = content.join(lines) print(content)6 Bạn cũng có thể sử dụng phương 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!")29 để đọc từng dòng một tệp, dừng lại khi bạn đến dòng mình muốn. Sử dụng kỹ thuật này, chúng tôi không cần phải đọc toàn bộ tệp ví dụ 3 print("Enter File's Name: ", end="") fname = input() fhandle = open(fname, "r") lines = fhandle.readlines() fhandle.close() print("Enter line number to replace it: ", end="") lno = int(input()) print("Enter new content for line no.", lno, "\b: ", end="") nline = input() print("\n------Old Content-------") content = "" content = content.join(lines) print(content) lines[lno] = nline fhandle = open(fname, "w") fhandle.writelines(lines) fhandle.close() print("\n-----New Content--------") content = "" content = content.join(lines) print(content)8 Trình tạo để đọc các dòng từ một tệp theo số dòngMột giải pháp nhanh và gọn có thể là biểu thức trình tạo để đọc tệp bằng cách sử dụng số dòng Nếu số lượng dòng được trả về từ một tệp lớn, bạn nên sử dụng trình tạo Giải pháp này chấp nhận con trỏ tệp và số dòng được đọc trả về một đối tượng trình tạo để lặp lại bằng vòng lặp để nhận từng dòng Có cách nào để đi đến một dòng cụ thể trong Python không?Khi bạn sử dụng câu lệnh goto trong Python, về cơ bản, bạn đang hướng dẫn trình thông dịch thực thi trực tiếp một dòng mã khác thay vì dòng mã hiện tại. Dòng mã mục tiêu mà bạn muốn trình thông dịch thực thi tại thời điểm này cần được đánh dấu trong phần có tên là “nhãn”.
Làm cách nào để chèn văn bản vào một vị trí cụ thể trong tệp của bạn bằng Python?phương thức seek()
. Bộ xử lý tệp giống như một con trỏ, xác định vị trí dữ liệu phải được đọc hoặc ghi trong tệp. |