How to quickly parse a list of strings

后端 未结 2 1929
星月不相逢
星月不相逢 2020-12-14 18:25

If I want to split a list of words separated by a delimiter character, I can use

>>> \'abc,foo,bar\'.split(\',\')
[\'abc\', \'foo\', \'bar\']


        
相关标签:
2条回答
  • 2020-12-14 19:02

    The CSV module should be able to do that for you

    0 讨论(0)
  • 2020-12-14 19:14
    import csv
    
    input = ['abc,"a string, with a comma","another, one"']
    parser = csv.reader(input)
    
    for fields in parser:
      for i,f in enumerate(fields):
        print i,f    # in Python 3 and up, print is a function; use: print(i,f)
    

    Result:

    0 abc
    1 a string, with a comma
    2 another, one
    
    0 讨论(0)
提交回复
热议问题