Bạn có thể thay thế ": \" bằng một cái gì đó [chỉ cần đảm bảo rằng đây là thứ không tồn tại trong chuỗi ở nơi khác ... bạn có thể sử dụng một thời gian dài hoặc một cái gì đó], và hơn là tách bởi ":" Và Thay thế nó trở lại.
[x.replace["$","\:"] for x in str1.replace["\:","$"].split[":"]]
Explanation:
str1 = 'I:would:like:to:find\:out:how:this\:works'
Thay thế ":" bằng "$" [hoặc một cái gì đó khác]:
str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
Bây giờ bị chia rẽ bởi ":"
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
và thay thế "$" bằng ":" cho mọi yếu tố:
[x.replace["$","\:"] for x in str1.replace["\:","$"].split[":"]]
Out: ['I', 'would', 'like', 'to', 'find\\:out', 'how', 'this\\:works']
Bài viết này mô tả cách phân chia các chuỗi theo phân định, ngắt dòng, biểu thức thông thường và số lượng ký tự trong Python.
- Chia theo dấu phân cách:
7str1.replace["\:","$"] Out: 'I:would:like:to:find$out:how:this$works'
- Chỉ định dấu phân cách:
8str1.replace["\:","$"] Out: 'I:would:like:to:find$out:how:this$works'
- Chỉ định số lượng phân tách tối đa:
9str1.replace["\:","$"] Out: 'I:would:like:to:find$out:how:this$works'
- Chỉ định dấu phân cách:
- Chia từ bên phải bởi DELIMITER:
0str1.replace["\:","$"].split[":"] Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
- Chia theo dòng phá vỡ:
1str1.replace["\:","$"].split[":"] Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
- Tách bằng regex:
2str1.replace["\:","$"].split[":"] Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
- Chia tách bởi nhiều trình phân cách khác nhau
- Concatenate một danh sách các chuỗi
- Chia dựa trên số lượng ký tự: Slice
Xem bài viết sau đây để biết thêm thông tin về cách kết hợp và trích xuất chuỗi.
- Chuỗi Concatenate trong Python [+ toán tử, tham gia, v.v.]
- Trích xuất một chuỗi con từ một chuỗi trong python [vị trí, regex]
Chia theo dấu phân cách: str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
7
str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
Chỉ định dấu phân cách:
str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
8- Chỉ định số lượng phân tách tối đa:
9str1.replace["\:","$"] Out: 'I:would:like:to:find$out:how:this$works'
Chia từ bên phải bởi DELIMITER:
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
0Chia theo dòng phá vỡ:
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
1s_blank = 'one two three\nfour\tfive'
print[s_blank]
# one two three
# four five
print[s_blank.split[]]
# ['one', 'two', 'three', 'four', 'five']
print[type[s_blank.split[]]]
#
Tách bằng regex:
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
2Chỉ định dấu phân cách: str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
8
str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
Chỉ định số lượng phân tách tối đa:
str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
9s_comma = 'one,two,three,four,five'
print[s_comma.split[',']]
# ['one', 'two', 'three', 'four', 'five']
print[s_comma.split['three']]
# ['one,two,', ',four,five']
Chia từ bên phải bởi DELIMITER:
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
0Chỉ định số lượng phân tách tối đa: str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
9
str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
Chia từ bên phải bởi DELIMITER:
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
0Chia theo dòng phá vỡ:
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
1print[s_comma.split[',', 2]]
# ['one', 'two', 'three,four,five']
Tách bằng regex:
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
2Chia tách bởi nhiều trình phân cách khác nhau
s_lines = 'one\ntwo\nthree\nfour'
print[s_lines]
# one
# two
# three
# four
print[s_lines.split['\n', 1]]
# ['one', 'two\nthree\nfour']
print[s_lines.split['\n', 1][0]]
# one
print[s_lines.split['\n', 1][1]]
# two
# three
# four
print[s_lines.split['\n', 1][-1]]
# two
# three
# four
Concatenate một danh sách các chuỗi
print[s_lines.split['\n', 2][-1]]
# three
# four
Chia từ bên phải bởi DELIMITER: str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
0
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
Chia theo dòng phá vỡ:
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
1- Tách bằng regex:
2str1.replace["\:","$"].split[":"] Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
Chia tách bởi nhiều trình phân cách khác nhau
Concatenate một danh sách các chuỗi
str1 = 'I:would:like:to:find\:out:how:this\:works'
0Chia dựa trên số lượng ký tự: Slice
str1 = 'I:would:like:to:find\:out:how:this\:works'
1Chia theo dòng phá vỡ: str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
1
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
Tách bằng regex:
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
2- Chia tách bởi nhiều trình phân cách khác nhau
Concatenate một danh sách các chuỗi
Chia dựa trên số lượng ký tự: Slice
Xem bài viết sau đây để biết thêm thông tin về cách kết hợp và trích xuất chuỗi.
str1 = 'I:would:like:to:find\:out:how:this\:works'
2Chuỗi Concatenate trong Python [+ toán tử, tham gia, v.v.]
str1 = 'I:would:like:to:find\:out:how:this\:works'
3Trích xuất một chuỗi con từ một chuỗi trong python [vị trí, regex]
str1 = 'I:would:like:to:find\:out:how:this\:works'
4Sử dụng phương thức
str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
7 để phân chia theo dấu phân cách.str1 = 'I:would:like:to:find\:out:how:this\:works'
5Nếu đối số đầu tiên,
s_comma = 'one,two,three,four,five'
print[s_comma.split[',']]
# ['one', 'two', 'three', 'four', 'five']
print[s_comma.split['three']]
# ['one,two,', ',four,five']
6, được đặt thành s_comma = 'one,two,three,four,five'
print[s_comma.split[',']]
# ['one', 'two', 'three', 'four', 'five']
print[s_comma.split['three']]
# ['one,two,', ',four,five']
7, kết quả bao gồm một ký tự mới ở cuối dòng.str1 = 'I:would:like:to:find\:out:how:this\:works'
6Xem bài viết sau đây cho các hoạt động khác với các lần phá vỡ dòng.
- Xử lý các lần phá vỡ dòng [Newlines] trong Python
Tách bằng regex: str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
2
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
7 và str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
0 chỉ phân chia khi str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
8 hoàn toàn phù hợp.Nếu bạn muốn chia một chuỗi phù hợp với biểu thức thông thường [regex] thay vì khớp hoàn hảo, hãy sử dụng
str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
7 của mô -đun RE.- Re.Split [] - Hoạt động biểu thức thường xuyên - Python 3.7.3 Tài liệu
Trong
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
2, chỉ định mẫu regex trong tham số đầu tiên và chuỗi ký tự đích trong tham số thứ hai.Một ví dụ về sự phân chia theo các số liên tiếp như sau.
str1 = 'I:would:like:to:find\:out:how:this\:works'
7Số lượng phân tách tối đa có thể được chỉ định trong tham số thứ ba,
str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
9.str1 = 'I:would:like:to:find\:out:how:this\:works'
8Chia tách bởi nhiều trình phân cách khác nhau
Hai phần sau đây rất hữu ích để nhớ ngay cả khi bạn không quen thuộc với Regex.
Kèm theo một chuỗi với
print[s_comma.split[',', 2]]
# ['one', 'two', 'three,four,five']
5 để phù hợp với bất kỳ ký tự nào trong đó. Bạn có thể phân chia chuỗi bằng nhiều ký tự khác nhau.str1 = 'I:would:like:to:find\:out:how:this\:works'
9Nếu các mẫu được phân định bởi
print[s_comma.split[',', 2]]
# ['one', 'two', 'three,four,five']
6, nó phù hợp với bất kỳ mẫu nào. Tất nhiên, có thể sử dụng các ký tự đặc biệt của regex cho mỗi mẫu, nhưng không sao ngay cả khi chuỗi thông thường được chỉ định như vậy. Bạn có thể chia bằng nhiều chuỗi khác nhau.str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
0Concatenate một danh sách các chuỗi
Trong các ví dụ trước, bạn có thể chia chuỗi và có danh sách.
Nếu bạn muốn kết hợp danh sách các chuỗi vào một chuỗi, hãy sử dụng phương thức chuỗi,
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
7.Gọi
str1.replace["\:","$"].split[":"]
Out: ['I', 'would', 'like', 'to', 'find$out', 'how', 'this$works']
7 từ print[s_comma.split[',', 2]]
# ['one', 'two', 'three,four,five']
9 và truyền một danh sách các chuỗi được kết hợp để tranh luận.str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
1Xem bài viết sau đây để biết chi tiết về nối chuỗi.
- Chuỗi Concatenate trong Python [+ toán tử, tham gia, v.v.]
Chia dựa trên số lượng ký tự: Slice
Sử dụng lát cắt để phân chia chuỗi dựa trên số lượng ký tự.
- Cách cắt một danh sách, chuỗi, tuple trong Python
str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
2Nó có thể được lấy như một tuple hoặc được gán cho một biến tương ứng.
- Nhiều gán trong Python: gán nhiều giá trị hoặc cùng một giá trị cho nhiều biến
str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
3
Chia thành ba:
str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
4Số lượng ký tự có thể thu được với chức năng tích hợp
s_lines = 'one\ntwo\nthree\nfour'
print[s_lines]
# one
# two
# three
# four
print[s_lines.split['\n', 1]]
# ['one', 'two\nthree\nfour']
print[s_lines.split['\n', 1][0]]
# one
print[s_lines.split['\n', 1][1]]
# two
# three
# four
print[s_lines.split['\n', 1][-1]]
# two
# three
# four
0. Nó cũng có thể được chia thành một nửa bằng cách sử dụng này.str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
5Nếu bạn muốn nối các chuỗi, hãy sử dụng toán tử
s_lines = 'one\ntwo\nthree\nfour'
print[s_lines]
# one
# two
# three
# four
print[s_lines.split['\n', 1]]
# ['one', 'two\nthree\nfour']
print[s_lines.split['\n', 1][0]]
# one
print[s_lines.split['\n', 1][1]]
# two
# three
# four
print[s_lines.split['\n', 1][-1]]
# two
# three
# four
1.str1.replace["\:","$"]
Out: 'I:would:like:to:find$out:how:this$works'
6