分割python字符串

编程入门 行业动态 更新时间:2024-10-24 11:21:19
本文介绍了分割python字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在python中有一个字符串,我想以一种非常特殊的方式进行拆分.我想将其拆分为包含每个单独单词的列表,但一组单词以特定字符为边界的情况除外.例如,以下字符串将被拆分.

I have a string in python that I want to split in a very particular manner. I want to split it into a list containing each separate word, except for the case when a group of words are bordered by a particular character. For example, the following strings would be split as such.

'Jimmy threw his ball through the window.'

成为

['Jimmy', 'threw', 'his', 'ball', 'through', 'the', 'window.']

但是,我想要带有边框字符

However, with a border character I'd want

'Jimmy |threw his ball| through the window.'

成为

['Jimmy', 'threw his ball', 'through', 'the', 'window.']

作为附加组件,我需要-,它可能出现在分组短语的外面,以便在拆分后出现在其中,

As an additional component I need - which may appear outside the grouping phrase to appear inside it after splitting up i.e.,

'Jimmy |threw his| ball -|through the| window.'

将成为

['Jimmy', 'threw his', 'ball', '-through the', 'window.']

在没有很多复杂的for循环和if语句的情况下,我找不到一种简单的,pythonic的方式来做到这一点.有没有简单的方法来处理这样的事情?

I cannot find a simple, pythonic way to do this without a lot of complicated for loops and if statements. Is there a simple way to handle something like this?

推荐答案

这不是开箱即用的解决方案,但这是一个非常像Python的函数,应该可以处理您扔给它的几乎所有内容

This isn't something with an out-of-the-box solution, but here's a function that's pretty Pythonic that should handle pretty much anything you throw at it.

def extract_groups(s): separator = repile("(-?\|[\w ]+\|)") components = separator.split(s) groups = [] for component in components: component = component.strip() if len(component) == 0: continue elif component[0] in ['-', '|']: groups.append(component.replace('|', '')) else: groups.extend(component.split(' ')) return groups

使用您的示例:

>>> extract_groups('Jimmy threw his ball through the window.') ['Jimmy', 'threw', 'his', 'ball', 'through', 'the', 'window.'] >>> extract_groups('Jimmy |threw his ball| through the window.') ['Jimmy', 'threw his ball', 'through the', 'window.'] >>> extract_groups('Jimmy |threw his| ball -|through the| window.') ['Jimmy', 'threw his', 'ball', '-through the', 'window.']

更多推荐

分割python字符串

本文发布于:2023-11-29 00:31:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!