【问题标题】:Python split string and keep delimiters as a word [duplicate]Python拆分字符串并将分隔符保留为一个单词[重复]
【发布时间】:2017-04-04 14:12:51
【问题描述】:

我正在尝试使用多个分隔符拆分字符串。我需要将分隔符保留为单词。 我使用的分隔符是:所有的标点符号和空格。

例如字符串:

Je suis, FOU et toi ?!

应该产生:

'Je'
'suis'
','
'FOU'
'et'
'toi'
'?'
'!'

我写道:

class Parser :
    def __init__(self) :
        """Empty constructor"""

    def read(self, file_name) :
        from string import punctuation
        with open(file_name, 'r') as file :
            for line in file :
                for word in line.split() :
                    r = re.compile(r'[\s{}]+'.format(re.escape(punctuation)))
                    print(r.split(word))

但我得到的结果是:

['Je']
['suis', '']
['FOU']
['et']
['toi']
['', '']

拆分似乎是正确的,但结果列表不包含分隔符:(

【问题讨论】:

    标签: python regex string split


    【解决方案1】:

    您需要将您的表达式放入re.split() 的组中以保留它。我不会先拆分空格;您以后可以随时删除仅限空格的字符串。如果您希望每个标点符号分开,那么您应该只在\s 空白组上使用+ 量词:

    # do this just once, not in a loop
    pattern = re.compile(r'(\s+|[{}])'.format(re.escape(punctuation)))
    
    # for each line
    parts = [part for part in pattern.split(line) if part.strip()]
    

    列表推导会删除仅包含空格的任何内容:

    >>> import re
    >>> from string import punctuation
    >>> line = 'Je suis, FOU et toi ?!'
    >>> pattern = re.compile(r'(\s+|[{}])'.format(re.escape(punctuation)))
    >>> pattern.split(line)
    ['Je', ' ', 'suis', ',', '', ' ', 'FOU', ' ', 'et', ' ', 'toi', ' ', '', '?', '', '!', '']
    >>> [part for part in pattern.split(line) if part.strip()]
    ['Je', 'suis', ',', 'FOU', 'et', 'toi', '?', '!']
    

    除了拆分之外,您还可以使用re.findall() 来查找所有单词的标点序列:

    pattern = re.compile(r'\w+|[{}]'.format(re.escape(punctuation)))
    
    parts = pattern.findall(line)
    

    这样做的好处是不需要过滤掉空格:

    >>> pattern = re.compile(r'\w+|[{}]'.format(re.escape(punctuation)))
    >>> pattern.findall(line)
    ['Je', 'suis', ',', 'FOU', 'et', 'toi', '?', '!']
    

    【讨论】:

      猜你喜欢
      • 2014-11-28
      • 2018-04-10
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 2022-11-03
      • 1970-01-01
      相关资源
      最近更新 更多