Hướng dẫn how do you remove spaces in a text file in python? - làm cách nào để xóa khoảng trắng trong tệp văn bản trong python?

Đọc văn bản khỏi tệp, xóa khoảng trống, ghi văn bản vào tệp:

with open('file.txt', 'r') as f: txt = f.read().replace(' ', '') with open('file.txt', 'w') as f: f.write(txt)

Trong giải pháp của @Leonardo Chirivì, việc tạo danh sách để lưu trữ nội dung tệp khi một chuỗi đủ và hiệu quả hơn. Hoạt động with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 0 chỉ được gọi một lần trên chuỗi, hiệu quả hơn so với lặp lại thông qua danh sách thực hiện thay thế cho từng dòng riêng lẻ.

Để tránh mở tệp hai lần:

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate()

Sẽ hiệu quả hơn khi chỉ mở tệp một lần. Điều này yêu cầu di chuyển con trỏ tệp trở lại đầu tệp sau khi đọc, cũng như cắt ngắn mọi nội dung còn lại còn lại sau khi bạn viết lại cho tệp. Tuy nhiên, một nhược điểm của giải pháp này là không dễ đọc.

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 8with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9 www.geeksforgeeks.org 7

Examples:

Input : g e e k Output : geek Input : Hello World Output : HelloWorld

www.geeksforgeeks.org 8with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9 with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 00with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 01geek4

Phương pháp 7: Xóa khoảng trống khỏi chuỗi bằng hàm rstrip ()sing replace() function

Python3

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 1 with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 2

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 3with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 4 geek2geek8______79

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 8with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9 Input : g e e k Output : geek Input : Hello World Output : HelloWorld0

Input : g e e k Output : geek Input : Hello World Output : HelloWorld1Input : g e e k Output : geek Input : Hello World Output : HelloWorld2

Đầu ra

geek

Phương pháp 5: Xóa khoảng trống khỏi chuỗi bằng cách sử dụng chức năng và câu lệnh có điều kiệnMethod 2: Remove spaces from a string using split() and join() 

Giảm chức năng lặp qua chuỗi và nó tham gia vào phần tử của chuỗi để kết quả nếu đó không phải là không gian trắng. & Nbsp;

Python3

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 1 with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 2

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 3with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 4 geek2geek8______79

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 8with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9 Input : g e e k Output : geek Input : Hello World Output : HelloWorld0

Input : g e e k Output : geek Input : Hello World Output : HelloWorld1Input : g e e k Output : geek Input : Hello World Output : HelloWorld2

Đầu ra

geek

Phương pháp 5: Xóa khoảng trống khỏi chuỗi bằng cách sử dụng chức năng và câu lệnh có điều kiệnMethod 3: Remove spaces from a string using Python regex 

Giảm chức năng lặp qua chuỗi và nó tham gia vào phần tử của chuỗi để kết quả nếu đó không phải là không gian trắng. & Nbsp;

Python3

geek9 geek0geek3 geek2

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 1 with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 2

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 3with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 4 geek2geek8______79

Phương pháp 6: Xóa khoảng trống khỏi chuỗi bằng hàm lstrip ()

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 8with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9 Input : g e e k Output : geek Input : Hello World Output : HelloWorld0

Input : g e e k Output : geek Input : Hello World Output : HelloWorld1Input : g e e k Output : geek Input : Hello World Output : HelloWorld2

Đầu ra

geek

Phương pháp 5: Xóa khoảng trống khỏi chuỗi bằng cách sử dụng chức năng và câu lệnh có điều kiệnMethod 4: Remove spaces from a string using translate() 

Giảm chức năng lặp qua chuỗi và nó tham gia vào phần tử của chuỗi để kết quả nếu đó không phải là không gian trắng. & Nbsp;

Python3

geek9 geek0geek3 geek2

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 1 with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 2

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 3with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 4 geek2geek8______79

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 8with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9 Input : g e e k Output : geek Input : Hello World Output : HelloWorld0

Input : g e e k Output : geek Input : Hello World Output : HelloWorld1Input : g e e k Output : geek Input : Hello World Output : HelloWorld2

Đầu ra

geek

Phương pháp 5: Xóa khoảng trống khỏi chuỗi bằng cách sử dụng chức năng và câu lệnh có điều kiệnsing reduce function and conditional statement

Giảm chức năng lặp qua chuỗi và nó tham gia vào phần tử của chuỗi để kết quả nếu đó không phải là không gian trắng. & Nbsp;

Python3

geek9 geek0geek3 geek2

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 1 with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 2

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 3with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 4 geek2geek8______79

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 8with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9 Input : g e e k Output : geek Input : Hello World Output : HelloWorld0

Input : g e e k Output : geek Input : Hello World Output : HelloWorld1Input : g e e k Output : geek Input : Hello World Output : HelloWorld2

Output:

geek

Phương pháp 6: Xóa khoảng trống khỏi chuỗi bằng hàm lstrip ()Remove spaces from a string using lstrip() function

Lstrip () tạo ra một chuỗi mới bằng cách xóa khoảng trắng khỏi chuỗi bên trái bên trái hoặc khoảng trắng hàng đầu của nó.

Python3

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 8with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9 www.geeksforgeeks.org 7

www.geeksforgeeks.org 8with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9 with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 00with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 01geek4

Input : g e e k Output : geek Input : Hello World Output : HelloWorld1with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 04

Đầu ra

geeksforgeeks.org www.

Phương pháp 5: Xóa khoảng trống khỏi chuỗi bằng cách sử dụng chức năng và câu lệnh có điều kiệnRemove spaces from a string using rstrip() function

Giảm chức năng lặp qua chuỗi và nó tham gia vào phần tử của chuỗi để kết quả nếu đó không phải là không gian trắng. & Nbsp;

Python3

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 8with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9 www.geeksforgeeks.org 7

www.geeksforgeeks.org 8with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9 with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 00with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 01geek4

Input : g e e k Output : geek Input : Hello World Output : HelloWorld1with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 04

Đầu ra

www.geeksforgeeks.org

Phương pháp 8: Sử dụng phương thức ISSPace ()

Python3

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 1 with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 2

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 3with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 18with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 20

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 3with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 22 with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 23with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 24 with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 25

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 26geeksforgeeks.org www.3geek8with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 29 with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 30

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 31with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 18geeksforgeeks.org www.1with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 35

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 3with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 4 with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 38

with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 8with open('file.txt', 'r+') as f: txt = f.read().replace(' ', '') f.seek(0) f.write(txt) f.truncate() 9 Input : g e e k Output : geek Input : Hello World Output : HelloWorld0

Input : g e e k Output : geek Input : Hello World Output : HelloWorld1Input : g e e k Output : geek Input : Hello World Output : HelloWorld2


Chủ đề