How can i parse a comma delimited string into a list (caveat)?

后端 未结 6 1043
心在旅途
心在旅途 2020-12-05 14:11

I need to be able to take a string like:

\'\'\'foo, bar, \"one, two\", three four\'\'\'

into:

[\'foo\', \'bar\', \'one, two         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 14:43

    If it doesn't need to be pretty, this might get you on your way:

    def f(s, splitifeven):
        if splitifeven & 1:
            return [s]
        return [x.strip() for x in s.split(",") if x.strip() != '']
    
    ss = 'foo, bar, "one, two", three four'
    
    print sum([f(s, sie) for sie, s in enumerate(ss.split('"'))], [])
    

提交回复
热议问题