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 regexImportRe#Import Regular Expression module Line='asdf fjdk; afed, FJEK,ASDF, foo'#(a) splitting on space, comma, and semicolonParts = Re.split (r'[;, \s]\s*', line)Print(Parts)#(b) using capturing groups in regular expression patterns, be aware that capturing groups are enclosed in parentheses, using capturing groups to cause matching text to also be included in the final resultFields = Re.split (r'(; |,|\s) \s*', line)Print(Fields)#(c) Improved output of strings based on the separator character aboveValues = Fields[::2]delimiters= Fields[1::2]delimiters.append ("')Print('value =', values)Print('delimiters =', delimiters) newline="'. Join (V+d forV,dinchzip (values, delimiters))Print('newline =', newline)#(d) Use of non-capturing groups (?: ...) The formal implementation of the regular expression pattern is grouped with parentheses, and the delimiter is not outputParts = Re.split (r'(?:,|;| \s) \s*', line)Print(parts)
>>> ================================ RESTART ================================>>> ['asdf','FJDK','afed','Fjek','asdf','Foo']['asdf',' ','FJDK',';','afed',',','Fjek',',','asdf',',','Foo']value= ['asdf','FJDK','afed','Fjek','asdf','Foo']delimiters= [' ',';',',',',',',',"']newline=asdf fjdk;afed,fjek,asdf,foo['asdf','FJDK','afed','Fjek','asdf','Foo']>>>
"Python Cookbook" "String and Text" 1. Splitting a string against any number of delimiters