Sử dụng phương pháp
import re6 để xóa dấu phẩy cuối cùng khỏi chuỗi, e. g.
import re7. Phương thức
import re6 sẽ trả về một bản sao của chuỗi đã xóa dấu phẩy ở cuối
Ví dụ đầu tiên sử dụng phương thức
import re6 để xóa ký tự cuối cùng khỏi chuỗi nếu đó là dấu phẩy
các str. phương thức rstrip lấy một chuỗi chứa các ký tự làm đối số và trả về một bản sao của chuỗi với các ký tự theo sau được chỉ định đã bị xóa
Phương thức không thay đổi chuỗi ban đầu, nó trả về một chuỗi mới. Các chuỗi là bất biến trong Python
Lưu ý rằng phương thức
import re6 chỉ xóa dấu phẩy nếu đó là ký tự cuối cùng trong chuỗi
Phương thức
import re6 sẽ xóa tất cả các dấu phẩy ở cuối chuỗi, không chỉ dấu phẩy cuối cùng
Ngoài ra, bạn có thể sử dụng phương pháp
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]2
Xóa dấu phẩy cuối cùng khỏi Chuỗi bằng cách sử dụng str. rsplit[] #
Để xóa dấu phẩy cuối cùng khỏi một chuỗi
- Sử dụng phương pháp
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
2 để tách chuỗi trên dấu phẩy, một lần, từ bên phải - Sử dụng phương thức
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
4 để nối danh sách thành một chuỗi
các str. phương thức rsplit trả về danh sách các từ trong chuỗi bằng cách sử dụng dấu tách được cung cấp làm chuỗi dấu phân cách
Phương thức nhận 2 đối số sau
NameDescriptionseparatorChia chuỗi thành các chuỗi con trên mỗi lần xuất hiện của dấu phân cáchmaxsplitCó tối đa 95 lần tách được thực hiện, những cái ngoài cùng bên phải [tùy chọn]Ngoại trừ tách từ bên phải,
import re70 hoạt động như
import re71
Bước cuối cùng là sử dụng phương thức
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]4 để nối danh sách thành một chuỗi
các str. phương thức tham gia lấy một iterable làm đối số và trả về một chuỗi là chuỗi nối của các chuỗi trong iterable
Python có rất nhiều hàm có sẵn giúp chúng ta thực hiện các thao tác khác nhau trên chuỗi. Một trong những chức năng tích hợp là chức năng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]9. Hàm này dùng để thay thế chuỗi này bằng chuỗi khác và trả về chuỗi mới. Những thay đổi sẽ được thực hiện được đề cập trong đối số chức năng
Ví dụ
sử dụng thay thế
1
2
3
4
5
initial_string = "re,move com,mas f,rom this, string"
Final_string = initial_string. thay thế[',',""]
print[Final_string]
đầu ra
xóa dấu phẩy khỏi chuỗi này
Giải trình
- Đã khởi tạo chuỗi mẫu thành
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
00 - Đã sử dụng hàm
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
9 để thay thế dấu phẩy trong chuỗi và gán nó cho một biến khácproverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
02 - Như bạn có thể thấy, chúng tôi đã thay thế dấu phẩy bằng chuỗi rỗng
- Đã in
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
02
Sử dụng chức năng gói proverb = 'In the valley, of the blind the one eyed man is king.'
result = proverb.replace[',', '']
print[result]
04 hoặc RegEx để xóa dấu phẩy khỏi chuỗi trong Python
Gói
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]04 hoặc RegEx là gói python tích hợp giúp xử lý các biểu thức chính quy. Gói này giúp thao tác các chuỗi ký tự và chuỗi đặc biệt
Chúng tôi có một câu tục ngữ nổi tiếng, được viết bởi Desiderius Erasmus, có chứa dấu phẩy hoặc dấu phẩy cần phải loại bỏ vì nó không có trong bản gốc. Vì vậy, hãy xem xét năm [5] cách để hoàn thành nhiệm vụ này
💬 câu hỏi. Làm cách nào chúng ta viết mã Python để xóa dấu phẩy/dấu phẩy khỏi Chuỗi?
Chúng ta có thể hoàn thành nhiệm vụ này bằng một trong các tùy chọn sau
- Phương pháp 1. Sử dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
6 - Phương pháp 2. Sử dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
7 - Phương pháp 3. Sử dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
8 vàproverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
9 - Phương pháp 4. Sử dụng
0 vàIn the valley of the blind the one-eyed man is king.
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
9 - Phương pháp 5. Sử dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
6 vàproverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
9 - Thưởng. Xóa dấu phẩy khỏi tệp văn bản
Sự chuẩn bị
Thêm đoạn mã sau vào đầu mỗi đoạn mã. Đoạn mã này sẽ cho phép mã trong bài viết này chạy không có lỗi
________số 8Phương pháp 1. Sử dụng thay thế[]
Phương pháp này sử dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]6 từ thư viện chuỗi tích hợp của Python để xóa dấu phẩy/dấu phẩy và trả về một chuỗi mới với các sửa đổi được thực hiện
ví dụ 1. Xóa tất cả Dấu phẩy khỏi Chuỗi
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]
Mã này khai báo một chuỗi chứa một dấu phẩy và lưu nó vào
In the valley of the blind the one-eyed man is king.
5Tiếp theo,
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]6 được thêm vào chuỗi [
In the valley of the blind the one-eyed man is king.
7] và các đối số được truyền cho chuỗi như sau- Đối số đầu tiên là ký tự cần thay thế/xóa [
8]In the valley of the blind the one-eyed man is king.
- Đối số thứ hai là ký tự để thay thế đối số thứ nhất bằng [______49]. Trong trường hợp này, một chuỗi rỗng
Cuối cùng,
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]60 được lưu và xuất ra thiết bị đầu cuối
đầu ra
In the valley of the blind the one-eyed man is king.
HOẶC
ví dụ 2. Xóa một dấu phẩy khỏi một chuỗi
Nếu chuỗi của bạn có hai [2] dấu phẩy trở lên thì sao?
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]6
Điều này có thể được thực hiện bằng cách chuyển đối số thứ ba cho
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]61 số lần xuất hiện [
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]62] để loại bỏ. Trong trường hợp này, dấu phẩy đầu tiên bị xóa và dấu phẩy khác vẫn còn [
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]63]
Cuối cùng,
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]60 được lưu và xuất ra thiết bị đầu cuối
💡 Lưu ý. Khi chuyển
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]62 sang
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]6, nó bắt đầu xóa số đã chỉ định của [các] ký tự đã chọn từ bên trái của chuỗi
đầu ra
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]5
Hoàn hảo
Phương thức chuỗi Python [Hướng dẫn cơ bản]
Xem video này trên YouTube
Phương pháp 2. Sử dụng Regex
Phương thức này gọi thư viện
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]67, loại bỏ tất cả các lần xuất hiện của ký tự được chỉ định và trả về một chuỗi mới có sửa đổi
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]7
Mã này khai báo một chuỗi chứa một dấu phẩy và lưu nó vào
In the valley of the blind the one-eyed man is king.
5Tiếp theo,
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]69 được gọi với ba [3] đối số
- Đối số đầu tiên là ký tự cần thay thế/xóa [
8]In the valley of the blind the one-eyed man is king.
- Đối số thứ hai là ký tự để thay thế đối số thứ nhất bằng [______49]. Trong trường hợp này, một chuỗi rỗng
- Đối số thứ ba là chuỗi để áp dụng điều này cho [______45]
Cuối cùng,
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]60 được lưu và xuất ra thiết bị đầu cuối
đầu ra
In the valley of the blind the one-eyed man is king.
💡 Lưu ý. Không giống như Phương pháp 1, phương pháp này không cung cấp tùy chọn nhập số lần xác định [số lần xuất hiện] để xóa. Trong trường hợp này, tất cả các lần xuất hiện sẽ bị xóa
5 kỹ năng phải có để trở thành lập trình viên [mà bạn chưa biết]
Xem video này trên YouTube
Phương pháp 3. Sử dụng hiểu danh sách
Phương pháp này sử dụng vòng lặp
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]8 và
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]55 để đánh giá, sau đó tách dòng trên ký tự đã chỉ định [
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]56] và loại bỏ ký tự tương tự. Một
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]57 được trả lại
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]9
Đoạn mã này khai báo một chuỗi chứa hai [2] dấu phẩy và lưu nó vào
In the valley of the blind the one-eyed man is king.
5Tiếp theo,
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]9 được gọi và truyền ký tự để tách chuỗi trên [
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]56]
Cuối cùng,
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]60 được lưu và xuất ra thiết bị đầu cuối ở định dạng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]57 chứa ba [3] phần tử
Giới thiệu đơn giản về hiểu danh sách trong Python
Xem video này trên YouTube
đầu ra
In the valley of the blind the one-eyed man is king.
5Phương pháp 4. Sử dụng Lambda và tách[]
Phương pháp này sử dụng một
In the valley of the blind the one-eyed man is king.
0 để lặp qua, proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]74 và
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]9 chuỗi trên một ký tự được chỉ định. Sau đó, [các] ký tự được chỉ định sẽ bị xóa và kết quả chuyển đổi thành đối tượng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]76, sau đó thành đối tượng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]57
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]1
Đoạn mã này khai báo một chuỗi chứa hai [2] dấu phẩy và lưu nó vào
In the valley of the blind the one-eyed man is king.
5Tiếp theo, một
In the valley of the blind the one-eyed man is king.
0 được sử dụng để proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]74 chuỗi và
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]9 trên ký tự dấu phẩy [
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]56], tạo ra ba [3] phần tử
Khi áp dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]76 cho
In the valley of the blind the one-eyed man is king.
0 , một đối tượng tương tự như sau được tạoproverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]0
Cuối cùng, đối tượng được chuyển đổi thành
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]57 và
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]60 được gửi đến thiết bị đầu cuối ở định dạng này có chứa ba [3] phần tử
Hãy chơi Finxter - Hàm Lambda trong Python
Xem video này trên YouTube
đầu ra
In the valley of the blind the one-eyed man is king.
5Phương pháp 5. Sử dụng thay thế[] và tách[]
Phương pháp đơn giản này sử dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]6 và
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]9 trên một dòng để xóa tất cả các lần xuất hiện của ký tự đã chỉ định [
In the valley of the blind the one-eyed man is king.
8] và trả về một proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]57
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]2
Đoạn mã này khai báo một chuỗi chứa hai [2] dấu phẩy và lưu nó vào
In the valley of the blind the one-eyed man is king.
5Tiếp theo,
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]92 được thêm vào
In the valley of the blind the one-eyed man is king.
5, thao tác này sẽ xóa [các] ký tự đã chỉ định khỏi chuỗi. Sau đó, proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]9 tách dòng trên ký tự đã chỉ định [______48]
Một danh sách chứa một [1] phần tử trả về
Cuối cùng, để xuất ra
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]60 mà không có dấu ngoặc xung quanh thì phải áp dụng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]97 vào cuối
đầu ra
In the valley of the blind the one-eyed man is king.
Thưởng. Xóa dấu phẩy khỏi tệp văn bản
Điều gì xảy ra nếu bạn muốn xóa một ký tự đã chỉ định khỏi tất cả các dòng trong tệp văn bản phẳng?
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]4
Nội dung tục ngữ. txt
In the valley of the blind the one-eyed man is king.
50Mã này mở và đọc nội dung của
In the valley of the blind the one-eyed man is king.
51 từng dòng mộtMỗi dòng được ước tính và ký tự được chỉ định sẽ bị xóa [______48]. Điều này được gọi một lần nữa vì chúng tôi cũng muốn xóa ký tự xuống dòng [‘
In the valley of the blind the one-eyed man is king.
53‘]Cuối cùng, đầu ra lưu vào
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]60 và được gửi đến thiết bị đầu cuối ở định dạng
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]57 chứa ba [3] phần tử
Python One-Liners - Thủ thuật 2 dòng Read File và Strip[]
Xem video này trên YouTube
đầu ra
proverb = 'In the valley, of the blind the one eyed man is king.' result = proverb.replace[',', ''] print[result]5
Tóm lược
Năm [5] phương pháp xóa dấu phẩy/dấu phẩy khỏi chuỗi này sẽ cung cấp cho bạn đủ thông tin để chọn phương pháp tốt nhất cho yêu cầu mã hóa của bạn