Đọ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 7Examples: 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()
01geek 4Phương pháp 7: Xóa khoảng trống khỏi chuỗi bằng hàm rstrip ()sing replace() functionPython3with 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()
2with 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 geek 2geek 8______79with 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 : HelloWorld 0Input : g e e k
Output : geek
Input : Hello World
Output : HelloWorld 1Input : 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()
2with 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 geek 2geek 8______79with 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 : HelloWorld 0Input : g e e k
Output : geek
Input : Hello World
Output : HelloWorld 1Input : 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; Python3geek 9 geek 0geek 3 geek 2with 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()
2with 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 geek 2geek 8______79Phươ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 : HelloWorld 0Input : g e e k
Output : geek
Input : Hello World
Output : HelloWorld 1Input : 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; Python3geek 9 geek 0geek 3 geek 2with 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()
2with 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 geek 2geek 8______79with 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 : HelloWorld 0Input : g e e k
Output : geek
Input : Hello World
Output : HelloWorld 1Input : 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 statementGiả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; Python3geek 9 geek 0geek 3 geek 2with 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()
2with 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 geek 2geek 8______79with 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 : HelloWorld 0Input : g e e k
Output : geek
Input : Hello World
Output : HelloWorld 1Input : g e e k
Output : geek
Input : Hello World
Output : HelloWorld 2Output: 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() functionLstrip () 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ó. Python3with 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 7www.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()
01geek 4Input : g e e k
Output : geek
Input : Hello World
Output : HelloWorld 1with 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() functionGiả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; Python3with 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 7www.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()
01geek 4Input : g e e k
Output : geek
Input : Hello World
Output : HelloWorld 1with 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 ()Python3with 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()
2with 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()
20with 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()
25with open('file.txt', 'r+') as f:
txt = f.read().replace(' ', '')
f.seek(0)
f.write(txt)
f.truncate()
26geeksforgeeks.org www. 3geek 8with 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()
30with 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()
35with 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()
38with 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 : HelloWorld 0Input : g e e k
Output : geek
Input : Hello World
Output : HelloWorld 1Input : g e e k
Output : geek
Input : Hello World
Output : HelloWorld 2 |