This article mainly describes the Python Cookbook (string and text) for any number of separators to split the string operation, in conjunction with an instance of the analysis of Python using split () and regular expressions for string splitting operation related implementation skills, the need for friends can refer to the following
The examples in this article describe Python splitting string operations against any number of delimiters. Share to everyone for your reference, as follows:
problem: splitting a string that is inconsistent between delimiters (and spaces between delimiters) into different fields;
Solution: Use the more flexible re.split () method, which can specify multiple patterns for delimiters.
Description: split () of a string object can handle only simple cases, and multiple separators are not supported, and there are no possible spaces around the delimiter.
# example.py## Example of splitting a string on multiple delimiters using a regeximport re #导入正则表达式模块line = ' asdf fjdk; Afed, FJEK,ASDF, foo ' # (a) splitting on space, comma, and semicolonparts = Re.split (R ' [;, \s]\s* ', line) print (parts) # ( b) using the capture group in the regular expression pattern, be aware that the capturing group is enclosed in parentheses, using capturing groups to cause matching text to also be included in the final result in fields = Re.split (R ' (; |,|\s) \s* ', line) print (fields) # (c) Improves the output of the string according to the separator character above values = Fields[::2]delimiters = Fields[1::2]delimiters.append (') print (' value = ', values) print (' delimiters = ', delimiters) newline = '. Join (v+d for v,d in zip (values, delimiters)) print (' newline = ', newline) # (d) using non-capturing groups (?:...) is implemented in parentheses to group the regular expression pattern, and does not output delimiters parts = Re.split (R ' (?:, |;| \s) \s* ', line) print (parts)
>>> ================================ RESTART ================================>>>[' asdf ', ' fjdk ', ' afed ', ' Fjek ', ' asdf ', ' foo ' [' asdf ', ' ', ' fjdk ', '; ', ' afed ', ', ', ' Fjek ', ', ', ' asdf ', ', ', ' foo ']value = [' asdf ', ' FJ ' DK ', ' afed ', ' Fjek ', ' asdf ', ' foo ']delimiters = [' ', '; ', ', ', ', ', ', ', ', ', ', ', ']newline = Asdf ' fjdk;afed,fjek,asdf,foo[' , ' fjdk ', ' afed ', ' Fjek ', ' asdf ', ' foo ']>>>
(Code from "Python Cookbook")