From skip at pobox.com  Thu Oct  6 04:21:06 2005
From: skip at pobox.com (skip at pobox.com)
Date: Wed, 5 Oct 2005 21:21:06 -0500
Subject: [Csv] PEP 305
In-Reply-To: <8EE4B60D3CFA10489B9CBA0D8BA64264021CC9ED@ATLEXMS01.na.mirant.net>
References: <8EE4B60D3CFA10489B9CBA0D8BA64264021CC9ED@ATLEXMS01.na.mirant.net>
Message-ID: <17220.35346.805469.431278@montanaro.dyndns.org>


    Moe> When I try to open a csv file from a website, the option to "open
    Moe> this file from its currebt location" is grayed out forcing me to
    Moe> download it to my hard drive in order to view it.

It's an issue for your web browser.  It may or may not allow you to specify
that files ending in '.csv' be opened with either your web browser or
another application.  If the web server maps CSV files to some MIME type
like application/octet-stream you may be out of luck though.

-- 
Skip Montanaro
Katrina Benefit Concerts: http://www.musi-cal.com/katrina
skip at pobox.com


From andrewm at object-craft.com.au  Thu Oct  6 04:01:17 2005
From: andrewm at object-craft.com.au (Andrew McNamara)
Date: Thu, 06 Oct 2005 12:01:17 +1000
Subject: [Csv] PEP 305 
In-Reply-To: <8EE4B60D3CFA10489B9CBA0D8BA64264021CC9ED@ATLEXMS01.na.mirant.net>
References: <8EE4B60D3CFA10489B9CBA0D8BA64264021CC9ED@ATLEXMS01.na.mirant.net>
Message-ID: <20051006020117.32AD76F4B25@longblack.object-craft.com.au>

>When I try to open a csv file from a website, the option to "open this
>file from its currebt location" is grayed out forcing me to download it to
>my hard drive in order to view it. Please help. Thanks.

I'm afraid I can't see how your question has anything to do with Python's
CSV module. 

-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/


From skip at pobox.com  Thu Oct  6 04:25:44 2005
From: skip at pobox.com (skip at pobox.com)
Date: Wed, 5 Oct 2005 21:25:44 -0500
Subject: [Csv] Request for enhancement for csv
In-Reply-To: <bbe9926e0509300153g35c020cepf939e18389f9da91@mail.gmail.com>
References: <bbe9926e0509300153g35c020cepf939e18389f9da91@mail.gmail.com>
Message-ID: <17220.35624.65118.728509@montanaro.dyndns.org>


    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


From paul1brian at gmail.com  Thu Oct  6 11:18:17 2005
From: paul1brian at gmail.com (paul brian)
Date: Thu, 6 Oct 2005 10:18:17 +0100
Subject: [Csv] Request for enhancement for csv
In-Reply-To: <17220.35624.65118.728509@montanaro.dyndns.org>
References: <bbe9926e0509300153g35c020cepf939e18389f9da91@mail.gmail.com>
	<17220.35624.65118.728509@montanaro.dyndns.org>
Message-ID: <bbe9926e0510060218k3b643836ge1178df746cb91bf@mail.gmail.com>

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


From skip at pobox.com  Fri Oct  7 19:22:29 2005
From: skip at pobox.com (skip at pobox.com)
Date: Fri, 7 Oct 2005 12:22:29 -0500
Subject: [Csv] Request for enhancement for csv
In-Reply-To: <bbe9926e0510060218k3b643836ge1178df746cb91bf@mail.gmail.com>
References: <bbe9926e0509300153g35c020cepf939e18389f9da91@mail.gmail.com>
	<17220.35624.65118.728509@montanaro.dyndns.org>
	<bbe9926e0510060218k3b643836ge1178df746cb91bf@mail.gmail.com>
Message-ID: <17222.44757.567848.351222@montanaro.dyndns.org>


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

    paul> Do you think it is likely to be worth submitting that as a patch

I'm ambivalent about it.  I've never needed that functionality before and
don't imagine many people will need it.  Also, it's not clear to me that the
KeyError exception raised when there's a mismatch between the expected and
actual column names is all that much worse than the ValueError I raised in
my example.  There's also the added complexity for the DictReader API.  In
your formulation the fieldnames argument is optional except when strict is
given.

If it was to go into the module, I'd propose that DictReader be subclassed
to implement your semantics.

Inputs from the other readers are always welcome as well.

Skip



From sjmachin at lexicon.net  Fri Oct  7 22:54:45 2005
From: sjmachin at lexicon.net (John Machin)
Date: Sat, 08 Oct 2005 06:54:45 +1000
Subject: [Csv] Request for enhancement for csv
In-Reply-To: <17222.44757.567848.351222@montanaro.dyndns.org>
References: <bbe9926e0509300153g35c020cepf939e18389f9da91@mail.gmail.com>	<17220.35624.65118.728509@montanaro.dyndns.org>	<bbe9926e0510060218k3b643836ge1178df746cb91bf@mail.gmail.com>
	<17222.44757.567848.351222@montanaro.dyndns.org>
Message-ID: <4346E095.4090806@lexicon.net>

skip at pobox.com wrote:

>    paul> Thats more or less what I do now (but have put the code in the csv
>    paul> module so its a one line call).
>
>    paul> Do you think it is likely to be worth submitting that as a patch
>
>I'm ambivalent about it.  I've never needed that functionality before and
>don't imagine many people will need it.  Also, it's not clear to me that the
>KeyError exception raised when there's a mismatch between the expected and
>actual column names is all that much worse than the ValueError I raised in
>my example.  There's also the added complexity for the DictReader API.  In
>your formulation the fieldnames argument is optional except when strict is
>given.
>
>If it was to go into the module, I'd propose that DictReader be subclassed
>to implement your semantics.
>
>Inputs from the other readers are always welcome as well.
>
>Skip
>
>  
>
My take: not in the module. What Paul wants to do is just *one* variant 
of column-name checking that I've used or seen in practice. Others: (a) 
case insensitive (b) some columns are mandatory, others optional with 
default values (c) some mandatory columns, others are "write your own 
ticket" columns i.e. the name is not known in advance. The ratio of time 
taken to code such stuff in Python to time taken to locate & parse the 
docs is too low IMHO.

Cheers,
John