Cách để trống dòng trong python

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 updates
0

Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
1

Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
2

Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
3

Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
4_______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 updates
9
Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
0

Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
1
Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
2

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 updates
3

Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
4_______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 updates
1
Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
8
Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
8# message to be written to the .txt file0# message to be written to the .txt file1
Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
5
Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
6

# message to be written to the .txt file4# message to be written to the .txt file5

Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
8
Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
9_______32_______8

# message to be written to the .txt file9

Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
1msg1 msg2msg3 msg4

# message to be written to the .txt file4msg6

# message to be written to the .txt file4msg8 msg9

=0____34_______1

Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
2

=3=

Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
5_______0_______6=7
Hello Programmers 
 GFG is the best platform 
 This file is a sample for code in Python
 Follow GFG website for more updates
8=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 updates
6'''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