CSV ignores lineterminator

Skip Montanaro skip at pobox.com
Mon Apr 5 15:48:38 EDT 2004


>>>>> "Jeffrey" == Jeffrey Barish <jeffbarish at starband.net> writes:

    Jeffrey> With
    Jeffrey> input_data = ['word1\tword2;word3\tword4;',
    Jeffrey> 'word5\tword6;word7\tword8;']

    Jeffrey> and

    Jeffrey> delimiter = '\t'
    Jeffrey> lineterminator = ';'

    Jeffrey> shouldn't csv.reader(input_data, dialect='mydialect') return

    Jeffrey> ['word1', 'word2']

    Jeffrey> as the first row?  I find that it doesn't matter how I set
    Jeffrey> lineterminator, csv always terminates at the end of the line returned
    Jeffrey> by the iterable object passed as its first argument (input_data, in
    Jeffrey> this case).  I must be missing something basic here.

    Jeffrey> I may be confused about the interaction between what iterable
    Jeffrey> object defines as the next row and what csv.reader defines as
    Jeffrey> the next row.

Perhaps.  Think of input_data as the conceptual result of
f.read().split(lineterminator) (though without loss of the line terminator):

    >>> input_data = ['word1\tword2;', 'word3\tword4;','word5\tword6;', 'word7\tword8;']
    >>> import csv
    >>> class d(csv.excel):
    ...   delimiter='\t'
    ...   lineterminator=';'
    ... 
    >>> rdr = csv.reader(input_data, dialect=d)
    >>> rdr.next()
    ['word1', 'word2;']
    >>> rdr.next()
    ['word3', 'word4;']
    >>> rdr.next()
    ['word5', 'word6;']
    >>> rdr.next()
    ['word7', 'word8;']

Skip




More information about the Python-list mailing list