Skip to content Skip to sidebar Skip to footer

Parse Comma Separated Csv File With Quotes In Python

Below i have a string which represents a single row pulled from a csv file. Each column is separated by a comma and the value is wrapped in ''. What is the simplest way to parse ou

Solution 1:

Python has a module for that:

http://docs.python.org/library/csv.html

import csv, sys
filename = 'some.csv'
with open(filename, 'rb') as f:
    reader = csv.reader(f)
    try:
        for row in reader:
            print row
    except csv.Error, e:
        sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))

Post a Comment for "Parse Comma Separated Csv File With Quotes In Python"