[Csv] Request for enhancement for csv

paul brian paul1brian at gmail.com
Thu Oct 6 11:18:17 CEST 2005


Skip,

Thank you for the reply.  Thats more or less what I do now (but have
put the code in the csv module so its a one line call).

Do you think it is likely to be worth submitting that as a patch (i
have got so used to the way i do it I do not know if it makes sense to
others?)

To be honest i think it barely qualifies as "patch".

yrs

paul

On 10/6/05, skip at pobox.com <skip at pobox.com> wrote:
>
>    paul> When opening a csv file using DictReader, I would like to be able
>    paul> to test that the file contains the correct header files as I
>    paul> expected.  This would prevent me from opening the incorrect file
>    paul> and seeing a indexError.
>
> Paul,
>
> Just specify your fieldnames when you open the file and check the keys and
> values of the first row, something like this:
>
>    import csv
>
>    rdr = csv.DictReader(open('somefile.csv', 'rb'),
>                         fieldnames=['column1', 'column2', 'column3'],
>                         ...)
>
>    headers = rdr.next()
>    if headers.keys() != headers.values():
>        raise ValueError("Unexpected header line, %s != %s",
>                         headers.keys(), headers.values())
>
> --
> Skip Montanaro
> Katrina Benefit Concerts: http://www.musi-cal.com/katrina
> skip at pobox.com
>


--
--------------------------
Paul Brian
m. 07875 074 534
t. 0208 352 1741



More information about the Csv mailing list