Skip to content Skip to sidebar Skip to footer

How To Quickly Parse A List Of Strings

If I want to split a list of words separated by a delimiter character, I can use >>> 'abc,foo,bar'.split(',') ['abc', 'foo', 'bar'] But how to easily and quickly do the s

Solution 1:

import csv

input = ['abc,"a string, with a comma","another, one"']
parser = csv.reader(input)

for fields in parser:
  for i,f inenumerate(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

Solution 2:

The CSV module should be able to do that for you

Post a Comment for "How To Quickly Parse A List Of Strings"