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()
8
with 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 
8
with 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()
00
with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
01
geek
4

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()
3
with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
4
geek
2
geek
8______79

with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
8
with 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 : HelloWorld
0

Input  : g e e k
Output : geek

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

Input  : Hello World
Output : HelloWorld
2

Đầ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()
3
with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
4
geek
2
geek
8______79

with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
8
with 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 : HelloWorld
0

Input  : g e e k
Output : geek

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

Input  : Hello World
Output : HelloWorld
2

Đầ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

geek
9
geek
0
geek
3
geek
2

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()
3
with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
4
geek
2
geek
8______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()
8
with 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 : HelloWorld
0

Input  : g e e k
Output : geek

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

Input  : Hello World
Output : HelloWorld
2

Đầ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

geek
9
geek
0
geek
3
geek
2

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()
3
with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
4
geek
2
geek
8______79

with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
8
with 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 : HelloWorld
0

Input  : g e e k
Output : geek

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

Input  : Hello World
Output : HelloWorld
2

Đầ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

geek
9
geek
0
geek
3
geek
2

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()
3
with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
4
geek
2
geek
8______79

with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
8
with 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 : HelloWorld
0

Input  : g e e k
Output : geek

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

Input  : Hello World
Output : HelloWorld
2

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()
8
with 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 
8
with 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()
00
with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
01
geek
4

Input  : g e e k
Output : geek

Input  : Hello World
Output : HelloWorld
1
with 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()
8
with 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 
8
with 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()
00
with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
01
geek
4

Input  : g e e k
Output : geek

Input  : Hello World
Output : HelloWorld
1
with 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()
3
with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
18
with 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()
20

with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
3
with 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()
23
with 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()
26
geeksforgeeks.org www.
3
geek
8
with 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()
31
with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
18
geeksforgeeks.org www.
1
with 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()
35

with open('file.txt', 'r+') as f:
    txt = f.read().replace(' ', '')
    f.seek(0)
    f.write(txt)
    f.truncate()
3
with 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()
8
with 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 : HelloWorld
0

Input  : g e e k
Output : geek

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

Input  : Hello World
Output : HelloWorld
2