Nhiều lần chúng ta gặp phải vấn đề cần xóa các dòng trống hoặc khoảng trống trong các dòng giữa văn bản của mình để văn bản trông có cấu trúc, ngắn gọn và có tổ chức hơn. Bài viết này sẽ đề cập đến hai phương pháp khác nhau để loại bỏ những dòng trống đó khỏi một. txt bằng mã Python
Điều này sẽ đạt được bằng cách sử dụng các phương pháp chính sau đây
- str. dải[ ]
- str. không gian [ ]
Tạo bài kiểm tra. tệp txt
Đầu tiên, chúng ta sẽ tạo một tệp trong. txt nơi chúng tôi sẽ để lại một số dòng bị thiếu hoặc trống có thể xóa sau. Đoạn mã sau được sử dụng để tạo và lưu tệp chỉ bằng Python
Python3
# message to be written to the .txt file
msg
=
'''Hello Programmers \
\n\n\n\n\n GFG is the best \
platform \n\n\n\n\n This \
file is a sample for code \
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates0
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates1
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates2
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates3
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates4_______0_______5_______0_______6_______0_______7_______0_______8
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates9
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates0
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates1
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates2
Bạn sẽ tìm thấy tệp văn bản được lưu trong hệ thống của mình có cùng tên. Sau đây là nội dung của. tập tin txt
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates
Làm thế nào để loại bỏ các dòng trống từ một. txt trong Python bằng str. dải [] Phương pháp
các str. Phương thức strip[] trong python loại bỏ tất cả các khoảng trắng xung quanh chuỗi đầu vào và trả về chuỗi kết quả
Chúng tôi sẽ sử dụng phương pháp sau để thực hiện nhiệm vụ mong muốn của chúng tôi
- Mở đầu vào và đầu ra. txt ở chế độ đọc và ghi [r, w]
- Sử dụng vòng lặp for để thực hiện phép lặp
- Sử dụng str. phương thức strip[] để kiểm tra xem dòng có trống không sau khi xóa tất cả khoảng trắng khỏi dòng
- Các dòng chỉ chứa khoảng trắng và chuỗi rỗng được lọc ra
- Sử dụng phương thức write[] để ghi kết quả của tệp
Sau đây là một triển khai của phương pháp trên
Python3
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates3
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates4_______0_______5_______0_______6
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates1
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates8
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates8
# message to be written to the .txt file
0# message to be written to the .txt file
1Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates5
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates6
# message to be written to the .txt file
4# message to be written to the .txt file
5
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates8
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates9_______32_______8
# message to be written to the .txt file
9
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates1
msg
1 msg
2msg
3 msg
4# message to be written to the .txt file
4msg
6
# message to be written to the .txt file
4msg
8 msg
9
=
0____34_______1
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates2
=
3=
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates5_______0_______6
=
7Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates8
=
9'''Hello Programmers \
0'''Hello Programmers \
1
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates6
'''Hello Programmers \
3'''Hello Programmers \
4đầu ra
Hello Programmers GFG is the best platform This file is a sample for code in Python Follow GFG website for more updates
Nói chung, dải [ ] xóa các ký tự từ cả bên trái và bên phải dựa trên đối số của chuỗi [chỉ định bộ ký tự sẽ bị xóa] nhưng chúng ta có thể thấy rằng không có đối số nào như vậy được cung cấp do đó dải [ ] sẽ xóa tất cả các ký tự . Đó là lý do tại sao tất cả các dòng trống đã bị xóa
Làm thế nào để loại bỏ các dòng trống từ một. txt trong Python bằng str. Phương thức isspace[]
các str. phương thức isspace[] kiểm tra xem ký tự được chỉ định có phải là khoảng trắng hay không và trả về một giá trị boolean