Splitting strings in Python using specific characters

后端 未结 3 703
暗喜
暗喜 2021-01-06 10:52

I\'m trying to split an inputted document at specific characters. I need to split them at [ and ] but I\'m having a difficult time figuring this out.

def mai         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-06 11:20

    str.split() splits at the exact string you pass to it, not at any of its characters. Passing "[]" would split at occurrences of [], but not at individual brackets. Possible solutions are

    1. splitting twice:

      words = [z for y in x.split("[") for z in y.split("]")]
      
    2. using re.split().

提交回复
热议问题