Splitting comma delimited strings in python

后端 未结 3 941
礼貌的吻别
礼貌的吻别 2021-01-12 02:08

This question has been asked and answered many times before. Some examples: [1], [2]. But there doesn\'t seem to be something somewhat more general. What I\'m looking for is

3条回答
  •  不思量自难忘°
    2021-01-12 02:29

    While it's not possible to use a Regular Expression, the following simple code will achieve the desired result:

    def split_at(text, delimiter, opens='<([', closes='>)]', quotes='"\''):
        result = []
        buff = ""
        level = 0
        is_quoted = False
    
        for char in text:
            if char in delimiter and level == 0 and not is_quoted:
                result.append(buff)
                buff = ""
            else:
                buff += char
    
                if char in opens:
                    level += 1
                if char in closes:
                    level -= 1
                if char in quotes:
                    is_quoted = not is_quoted
    
        if not buff == "":
            result.append(buff)
    
        return result
    

    Running this in the interpreter:

    >>> split_at('obj<1, 2, 3>, x(4, 5), "msg, with comma"', ',')                                                                                                                                 
    #=>['obj<1, 2, 3>', ' x(4, 5)', ' "msg with comma"']
    

提交回复
热议问题