Đọ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.
Đưa ra một chuỗi, hãy viết một chương trình Python để loại bỏ tất cả các không gian khỏi nó.
Examples:
Input : g e e k Output : geek Input : Hello World Output : HelloWorld
Có nhiều cách tiếp cận khác nhau để loại bỏ khoảng trắng trong một chuỗi. Cách đầu tiên là cách tiếp cận ngây thơ, đã được thảo luận trong bài viết này. Nhưng ở đây chúng tôi sẽ thảo luận về tất cả các phương pháp cụ thể cho Python.
Phương pháp 1: Xóa khoảng trống khỏi chuỗi bằng cách sử dụng hàm thay thế []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[]
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 with open['file.txt', 'r+'] as f:
txt = f.read[].replace[' ', '']
f.seek[0]
f.write[txt]
f.truncate[]
5with open['file.txt', 'r+'] as f:
txt = f.read[].replace[' ', '']
f.seek[0]
f.write[txt]
f.truncate[]
6with open['file.txt', 'r+'] as f:
txt = f.read[].replace[' ', '']
f.seek[0]
f.write[txt]
f.truncate[]
7with 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 : HelloWorld1
Input : g e e k Output : geek Input : Hello World Output : HelloWorld2
Đầu ra
geek
& nbsp; Phương thức & nbsp; 2: Xóa khoảng trống khỏi chuỗi bằng cách sử dụng split [] và tham gia [] & nbsp;Method 2: Remove spaces from a string using split[] and join[]
Đầu tiên, chúng tôi sử dụng hàm Split [] để trả về danh sách các từ trong chuỗi, sử dụng SEP làm chuỗi python dấu phân cách. Sau đó, chúng tôi sử dụng Jop [] để kết hợp với Itable. & 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 Input : g e e k Output : geek Input : Hello World Output : HelloWorld7
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 : HelloWorld1
Input : g e e k Output : geek Input : Hello World Output : HelloWorld2
Đầu ra
geek
& nbsp; Phương thức & nbsp; 2: Xóa khoảng trống khỏi chuỗi bằng cách sử dụng split [] và tham gia [] & nbsp;Method 3: Remove spaces from a string using Python regex
Đầu tiên, chúng tôi sử dụng hàm Split [] để trả về danh sách các từ trong chuỗi, sử dụng SEP làm chuỗi python dấu phân cách. Sau đó, chúng tôi sử dụng Jop [] để kết hợp với Itable. & NBSP;
Python3
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 Input : g e e k Output : geek Input : Hello World Output : HelloWorld7
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& nbsp; Phương thức & nbsp; 3: Xóa khoảng trống khỏi chuỗi bằng Python Regex & NBSP;
Để tìm một chuỗi hoặc nhóm chuỗi, một biểu thức chính quy [regex] là một chuỗi các ký tự duy nhất. Nó có thể so sánh một văn bản với một mẫu để xác định xem nó có mặt hay không. Nó cũng có thể chia một mẫu thành một hoặc nhiều mẫu phụ.
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 : HelloWorld1
Input : g e e k Output : geek Input : Hello World Output : HelloWorld2
Đầu ra
geek
& nbsp; Phương thức & nbsp; 2: Xóa khoảng trống khỏi chuỗi bằng cách sử dụng split [] và tham gia [] & nbsp;Method 4: Remove spaces from a string using translate[]
Đầu tiên, chúng tôi sử dụng hàm Split [] để trả về danh sách các từ trong chuỗi, sử dụng SEP làm chuỗi python dấu phân cách. Sau đó, chúng tôi sử dụng Jop [] để kết hợp với Itable. & NBSP;
Python3
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 Input : g e e k Output : geek Input : Hello World Output : HelloWorld7
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& nbsp; Phương thức & nbsp; 3: Xóa khoảng trống khỏi chuỗi bằng Python Regex & NBSP;
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 : HelloWorld1
Input : g e e k Output : geek Input : Hello World Output : HelloWorld2
Đầu ra
geek
& nbsp; Phương thức & nbsp; 2: Xóa khoảng trống khỏi chuỗi bằng cách sử dụng split [] và tham gia [] & nbsp;sing reduce function and conditional statement
Đầu tiên, chúng tôi sử dụng hàm Split [] để trả về danh sách các từ trong chuỗi, sử dụng SEP làm chuỗi python dấu phân cách. Sau đó, chúng tôi sử dụng Jop [] để kết hợp với Itable. & NBSP;
Python3
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 Input : g e e k Output : geek Input : Hello World Output : HelloWorld7
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& nbsp; Phương thức & nbsp; 3: Xóa khoảng trống khỏi chuỗi bằng Python Regex & NBSP;
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 : HelloWorld1
Input : g e e k Output : geek Input : Hello World Output : HelloWorld2
Output:
geek
Đầu raRemove spaces from a string using lstrip[] function
& nbsp; Phương thức & nbsp; 2: Xóa khoảng trống khỏi chuỗi bằng cách sử dụng split [] và tham gia [] & nbsp;
Python3
Đầu tiên, chúng tôi sử dụng hàm Split [] để trả về danh sách các từ trong chuỗi, sử dụng SEP làm chuỗi python dấu phân cách. Sau đó, chúng tôi sử dụng Jop [] để kết hợp với Itable. & NBSP;
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 Input : g e e k Output : geek Input : Hello World Output : HelloWorld7
Input : g e e k Output : geek Input : Hello World Output : HelloWorld1
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.
& nbsp; Phương thức & nbsp; 2: Xóa khoảng trống khỏi chuỗi bằng cách sử dụng split [] và tham gia [] & nbsp;Remove spaces from a string using rstrip[] function
Đầu tiên, chúng tôi sử dụng hàm Split [] để trả về danh sách các từ trong chuỗi, sử dụng SEP làm chuỗi python dấu phân cách. Sau đó, chúng tôi sử dụng Jop [] để kết hợp với Itable. & NBSP;
Python3
Đầu tiên, chúng tôi sử dụng hàm Split [] để trả về danh sách các từ trong chuỗi, sử dụng SEP làm chuỗi python dấu phân cách. Sau đó, chúng tôi sử dụng Jop [] để kết hợp với Itable. & NBSP;
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 Input : g e e k Output : geek Input : Hello World Output : HelloWorld7
Input : g e e k Output : geek Input : Hello World Output : HelloWorld1
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
& nbsp; Phương thức & nbsp; 2: Xóa khoảng trống khỏi chuỗi bằng cách sử dụng split [] và tham gia [] & 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[]
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Đầu tiên, chúng tôi sử dụng hàm Split [] để trả về danh sách các từ trong chuỗi, sử dụng SEP làm chuỗi python dấu phân cách. Sau đó, chúng tôi sử dụng Jop [] để kết hợp với Itable. & NBSP;
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 Input : g e e k Output : geek Input : Hello World Output : HelloWorld7
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.1
with 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& nbsp; Phương thức & nbsp; 3: Xóa khoảng trống khỏi chuỗi bằng Python Regex & NBSP;
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 : HelloWorld1
Input : g e e k Output : geek Input : Hello World Output : HelloWorld2