[Csv] CSV interface question

Skip Montanaro skip at pobox.com
Wed Jan 29 19:17:39 CET 2003


    Cliff> You've lost me, I'm afraid.  What I'm saying is that:

    Cliff> csvreader = reader(file("test_data/sfsample.csv", 'r'),
    Cliff>                    dialect='excel')

    Cliff> isn't as flexible as

    Cliff> csvreader = reader(file("test_data/sfsample.csv", 'r'),
    Cliff>                    dialect=excel)

    Cliff> where excel is either a pre-defined dictionary/class or a
    Cliff> user-created dictionary/class.

Yes, but my string just indexes into a mapping to get to the real dict which
stores the parameter settings, as I indicated in an earlier post:

    I was thinking of dialects as dicts.  You'd have

        excel_dialect = { "quotechar": '"',
                          "delimiter": ',',
                          "linetermintor": '\r\n',
                          ...
                        }

    with a corresponding mapping as you suggested:

        settings = { 'excel': excel_dialect,
                     'excel-tsv: excel_tabs_dialect, }

    then in the factory functions do something like:

        def reader(fileobj, dialect="excel", **kwds):
            kwargs = copy.copy(settings[dialect])
            kwargs.update(kwds)
            # possible sanity check on kwargs here ...
            return _csv.reader(fileobj, **kwargs)

Did that not make it out?  I also think it's cleaner if we have a data file
which is loaded at import time to define the various dialects.  That way we
aren't mixing too much data into our code.  It also opens up the opportunity
for users to later specify their own dialect data files.  Where I indicated
"possible sanity check" above would be a call to a validation function on
the settings.

Skip
_______________________________________________
Csv mailing list
Csv at mail.mojam.com
http://manatee.mojam.com/mailman/listinfo/csv



More information about the Csv mailing list