Python Cookbook (string and text) split string operation for any number of delimiters

Source: Internet
Author: User
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")

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.