Hướng dẫn split string based on delimiter python - phân tách chuỗi dựa trên dấu phân tách python

Phương thức phân chia chuỗi python trong Python chia một chuỗi thành một danh sách các chuỗi sau khi phá chuỗi đã cho bằng cách phân cách được chỉ định. in Python split a string into a list of strings after breaking the given string by the specified separator.

Phương thức phân chia chuỗi Python

Cú pháp: str.split (phân tách, MaxSplit) str.split(separator, maxsplit)

Thông số :

  • DEVERATOR: Đây là một dấu phân cách. Chuỗi chia tách tại phân tách được chỉ định này. Nếu không được cung cấp thì bất kỳ không gian trắng là một dấu tách. This is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator.
  • MAXSplit: Đó là một số, cho chúng ta biết chia chuỗi thành tối đa số lần được cung cấp. Nếu nó không được cung cấp thì mặc định là -1 có nghĩa là không có giới hạn. It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then the default is -1 that means there is no limit.

Trả về: Trả về một danh sách các chuỗi sau khi phá chuỗi đã cho bởi bộ phân cách được chỉ định. Returns a list of strings after breaking the given string by the specified separator.

Ví dụ Phương thức phân chia chuỗi Python

Python3

['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']5['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']6 ['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']7

['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']8['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']6 s_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())) # <class 'list'> 0s_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())) # <class 'list'> 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())) # <class 'list'> 2

s_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())) # <class 'list'> 3s_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())) # <class 'list'> 4

Output:

['one', 'two', 'three']

Ví dụ 1: Ví dụ để chứng minh chức năng Split () hoạt động như thế nào Example to demonstrate how split() function works

Ở đây chúng tôi đang sử dụng chức năng phân chia chuỗi python () để chia các chuỗi khác nhau thành một danh sách, được phân tách bởi các ký tự khác nhau trong mỗi trường hợp.

Python3

s_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())) # <class 'list'> 5['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']6 s_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())) # <class 'list'> 7

s_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())) # <class 'list'> 3s_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())) # <class 'list'> 9

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'] 0['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']6 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'] 2

s_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())) # <class 'list'> 3s_comma = 'one,two,three,four,five' print(s_comma.split(',')) # ['one', 'two', 'three', 'four', 'five'] print(s_comma.split('three')) # ['one,two,', ',four,five'] 4s_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())) # <class 'list'> 1s_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

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'] 0['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']6 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'] 9

s_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())) # <class 'list'> 3s_comma = 'one,two,three,four,five' print(s_comma.split(',')) # ['one', 'two', 'three', 'four', 'five'] print(s_comma.split('three')) # ['one,two,', ',four,five'] 4print(s_comma.split(',', 2)) # ['one', 'two', 'three,four,five'] 2s_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

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'] 0['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']6 print(s_comma.split(',', 2)) # ['one', 'two', 'three,four,five'] 6

s_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())) # <class 'list'> 3s_comma = 'one,two,three,four,five' print(s_comma.split(',')) # ['one', 'two', 'three', 'four', 'five'] print(s_comma.split('three')) # ['one,two,', ',four,five'] 4print(s_comma.split(',', 2)) # ['one', 'two', 'three,four,five'] 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'] 6

Đầu ra:

['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']

Ví dụ 2: Ví dụ để chứng minh chức năng phân tách () hoạt động như thế nào khi MaxSplit được chỉ định Example to demonstrate how split() function works when maxsplit is specified

Tham số MaxSplit được sử dụng để kiểm soát số lượng phân tách để trả về sau khi chuỗi được phân tích cú pháp. Ngay cả khi có nhiều lần phân tách có thể, nó sẽ chỉ thực hiện tối đa số lượng phân tách theo định nghĩa của tham số MaxSplit.

Python3

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'] 0['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']6 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 3

s_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())) # <class 'list'> 3s_comma = 'one,two,three,four,five' print(s_comma.split(',')) # ['one', 'two', 'three', 'four', 'five'] print(s_comma.split('three')) # ['one,two,', ',four,five'] 4s_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 6s_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 7s_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 8s_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

s_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())) # <class 'list'> 3s_comma = 'one,two,three,four,five' print(s_comma.split(',')) # ['one', 'two', 'three', 'four', 'five'] print(s_comma.split('three')) # ['one,two,', ',four,five'] 4s_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 6s_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 7print(s_lines.split('\n', 2)[-1]) # three # four 4s_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

s_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())) # <class 'list'> 3s_comma = 'one,two,three,four,five' print(s_comma.split(',')) # ['one', 'two', 'three', 'four', 'five'] print(s_comma.split('three')) # ['one,two,', ',four,five'] 4s_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 6s_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 7print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 0s_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

Đầu ra:

['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']

Phương thức chuỗi

Thí dụ

Chia một chuỗi thành một danh sách trong đó mỗi từ là một mục danh sách:

TXT = "Chào mừng đến với rừng rậm"

x = txt.split ()

in (x)

Hãy tự mình thử »

Định nghĩa và cách sử dụng

Phương thức print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 2 chia một chuỗi vào một danh sách.

Bạn có thể chỉ định phân tách, dấu phân cách mặc định là bất kỳ khoảng trắng nào.

Lưu ý: Khi MaxSplit được chỉ định, danh sách sẽ chứa số lượng phần tử được chỉ định cộng với một. When maxsplit is specified, the list will contain the specified number of elements plus one.

Cú pháp

String.Split (phân tách, MaxSplit)

Giá trị tham số

Tham sốSự mô tả
máy tách biệtKhông bắt buộc. Chỉ định phân tách để sử dụng khi chia chuỗi. Theo mặc định, bất kỳ khoảng trắng nào cũng là một dấu tách
MaxSplitKhông bắt buộc. Chỉ định số lượng để làm. Giá trị mặc định là -1, là "tất cả các lần xuất hiện"

Nhiều ví dụ hơn

Thí dụ

Chia một chuỗi thành một danh sách trong đó mỗi từ là một mục danh sách:

TXT = "Chào mừng đến với rừng rậm"

x = txt.split ()

in (x)

Hãy tự mình thử »

Thí dụ

Chia một chuỗi thành một danh sách trong đó mỗi từ là một mục danh sách:

TXT = "Chào mừng đến với rừng rậm"

x = txt.split ()

in (x)

Hãy tự mình thử »

Thí dụ

Chia một chuỗi thành một danh sách trong đó mỗi từ là một mục danh sách:

TXT = "Chào mừng đến với rừng rậm"

x = txt.split ()
x = txt.split("#", 1)

in (x)

Hãy tự mình thử »

Phương thức chuỗi


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: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 2
    • Chỉ định dấu phân cách: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 4
    • Chỉ định số lượng phân tách tối đa: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 5
  • Chia từ bên phải bởi DELIMITER: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 6
  • Chia theo dòng phá vỡ: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 7
  • Tách bằng regex: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 8
    • 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

Sử dụng lát cắt để phân chia chuỗi dựa trên số lượng ký tự.

  • Chuỗi Concatenate trong Python (+ toán tử, tham gia, v.v.)
  • Chia dựa trên số lượng ký tự: Slice

Chia theo dấu phân cách: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 2

Chỉ định dấu phân cách: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 4

  • Chỉ định số lượng phân tách tối đa: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 5

Chia từ bên phải bởi DELIMITER: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 6

Một danh sách các từ được trả về.

s_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())) # <class 'list'>

Sử dụng print(s_lines.rsplit('\n', 2)[0]) # one # two 3, được mô tả bên dưới, để kết hợp danh sách thành một chuỗi.

Chỉ định dấu phân cách: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 4

Chỉ định một dấu phân cách cho tham số đầu tiên print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 4.

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']

Nếu bạn muốn chỉ định nhiều dấu phân cách, hãy sử dụng các biểu thức chính quy như mô tả sau.

Chỉ định số lượng phân tách tối đa: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 5

Chỉ định số lượng phân tách tối đa cho tham số thứ hai print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 5.

Nếu print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 5 được đưa ra, nhiều nhất, việc chia tách print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 5 được thực hiện.

print(s_comma.split(',', 2)) # ['one', 'two', 'three,four,five']

Ví dụ, rất hữu ích khi xóa dòng đầu tiên khỏi một chuỗi.

Nếu ['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']00, ['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']01, bạn có thể nhận được một danh sách các chuỗi được phân chia theo ký tự dòng mới đầu tiên print(s_lines.rsplit('\n', 2)[0]) # one # two 1. Phần tử thứ hai ['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']03 của danh sách này là một chuỗi không bao gồm dòng đầu tiên. Vì nó là phần tử cuối cùng, nó có thể được chỉ định là ['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']04.

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

Tương tự, để xóa hai dòng đầu tiên:

print(s_lines.split('\n', 2)[-1]) # three # four

Chia từ bên phải bởi DELIMITER: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 6

print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 6 phân tách từ bên phải của chuỗi.

  • str.rsplit () - Python 3.7.3 Tài liệu

Kết quả khác với print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 2 chỉ khi tham số thứ hai print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 5 được đưa ra.

Theo cách tương tự như print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 2, nếu bạn muốn xóa dòng cuối cùng, hãy sử dụng print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 6.

print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four

Để xóa hai dòng cuối cùng:

print(s_lines.rsplit('\n', 2)[0]) # one # two

Chia theo dòng phá vỡ: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 7

Ngoài ra còn có một print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 7 để phân tách theo ranh giới dòng.

  • Str.SplitLines () - Python 3.7.3 Tài liệu

Như trong các ví dụ trước, print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 2 và print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 6 được chia theo mặc định với khoảng trắng bao gồm ngắt dòng và bạn cũng có thể chỉ định ngắt dòng với tham số print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 4.

Tuy nhiên, thường tốt hơn là sử dụng print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 7.

Ví dụ: Chuỗi phân chia chứa print(s_lines.rsplit('\n', 2)[0]) # one # two 1 (LF, được sử dụng trong HĐH UNIX bao gồm Mac) và ['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']18 (Cr + LF, được sử dụng trong OS Windows).

['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']0

Khi print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 2 được áp dụng, theo mặc định, nó được phân chia không chỉ bởi các dòng phá vỡ mà còn bởi không gian.

['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']1

Vì chỉ có một ký tự mới có thể được chỉ định trong print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 4, nên không thể chia nếu có các ký tự dòng hỗn hợp. Nó cũng được chia ở cuối nhân vật Newline.

['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']2

print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 7 chia tách tại các ký tự dòng khác nhau nhưng không phải ở các khoảng trắng khác.

['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']3

Nếu đối số đầu tiên, ['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']22, được đặt thành ['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']23, kết quả bao gồm một ký tự dòng mới ở cuối dòng.

['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']4

Xem 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: print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 8

print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 2 và print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 6 chỉ phân chia khi print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 4 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ì kết hợp hoàn hảo, hãy sử dụng print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 2 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 print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 8, 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.

['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']5

Số lượng phân tách tối đa có thể được chỉ định trong tham số thứ ba, print(s_lines.rsplit('\n', 1)) # ['one\ntwo\nthree', 'four'] print(s_lines.rsplit('\n', 1)[0]) # one # two # three print(s_lines.rsplit('\n', 1)[1]) # four 5.

['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']6

Chia 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 ['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']31 để 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.

['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']7

Nếu các mẫu được phân định bởi ['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']32, 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.

['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']8

Concatenate 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, print(s_lines.rsplit('\n', 2)[0]) # one # two 3.

Gọi print(s_lines.rsplit('\n', 2)[0]) # one # two 3 từ ['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']35 và vượt qua danh sách các chuỗi được nối với đối số.

['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']9

Xem 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

['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']0

Nó 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

['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']1

Chia thành ba:

['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']2

Số lượng ký tự có thể thu được với chức năng tích hợp ['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']36. Nó cũng có thể được chia thành một nửa bằng cách sử dụng này.

['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']3

Nếu bạn muốn nối các chuỗi, hãy sử dụng toán tử ['geeks', 'for', 'geeks'] ['geeks', ' for', ' geeks'] ['geeks', 'for', 'geeks'] ['Ca', 'Ba', 'Sa', 'Fa', 'Or']37.

['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks', 'for, geeks, pawan']4

Làm thế nào để bạn chia một chuỗi với một dấu phân cách trong Python?

Chia chuỗi trong Python (Delimiter, Line Break, Regex, v.v.)..
Chia theo DELIMITER: Split () Chỉ định DELIMITER: SEP. ....
Chia từ bên phải bằng Delimiter: RSplit ().
Chia theo dòng phá vỡ: Splitlines ().
Tách bằng regex: re.split () ....
Concatenate một danh sách các chuỗi ..
Chia dựa trên số lượng ký tự: Slice ..

Tôi có thể chia một chuỗi bằng hai dấu phân cách python không?

Để phân chia một chuỗi với nhiều trình phân cách trong Python, hãy sử dụng phương thức re.split ().Ở đó.Chức năng chia () chia chuỗi theo từng lần xuất hiện của mẫu.use the re. split() method. The re. split() function splits the string by each occurrence of the pattern.

Làm thế nào để bạn chia một chuỗi thành một danh sách bằng cách sử dụng dấu phân cách dấu phẩy?

Sử dụng str.split () để chuyển đổi chuỗi được phân tách bằng dấu phẩy thành danh sách.Gọi str.Chia (SEP) với "", như SEP để chuyển đổi chuỗi được phân tách bằng dấu phẩy thành một danh sách. split() to convert a comma-separated string to a list. Call str. split(sep) with "," as sep to convert a comma-separated string into a list.

Làm thế nào để bạn chia một chuỗi thành hai chuỗi trong Python?

Sử dụng chức năng split () Hàm này chia chuỗi thành các phần nhỏ hơn.Đây là điều ngược lại với việc hợp nhất nhiều chuỗi thành một.Hàm chia () chứa hai tham số. This function splits the string into smaller sections. This is the opposite of merging many strings into one. The split () function contains two parameters.

Chủ đề