CSV module - reading single line files?

Hello all

I'm using the csv module to read text files, but when I try to open a
single-line file, I can't retrieve any data.

I'm using this:

            sample = csvfile.read( 1024 )
            csvfile.seek( 0 )

            # Check for file format with sniffer.
            dialect = csv.Sniffer().sniff(sample)
            csvfile = csv.reader( csvfile, dialect )
            # Grab a sample and see if there is a header.
            if csv.Sniffer().has_header( sample ): #if there is a header
                print 'File has header!'
                colnames = csvfile.next() # label columns from first line
                datalist = list( csvfile ) # append data to a list
            else: #if there is NO header
                print 'NO header!'
                datalist = list( csvfile ) # append data to a list
                colnames = ['col_%i' % i for i in
range(len(datalist[0]))] # label columns as col_1, col_2, etc

If I have a single line file like:

58 67

and open it, I get the msg saying the file HAS header, and no data at
all. It work if I add a real header, but that wasn't the idea.

anyone experienced this before?

TIA

Carlos

···

--
Carlos Henrique Grohmann - Geologist D.Sc.
a.k.a. Guano - Linux User #89721
ResearcherID: A-9030-2008
carlos dot grohmann at gmail dot com
http://www.igc.usp.br/pessoais/guano/
_________________
"Good morning, doctors. I have taken the liberty of removing Windows
95 from my hard drive."
--The winning entry in a "What were HAL's first words" contest judged
by 2001: A SPACE ODYSSEY creator Arthur C. Clarke

Can’t stop the signal.

The "Sniffer" uses heuristics to try to guess your format. It is not
omniscient. In particular, if you don't give it enough information to
figure out the format, it has to guess. In your case, given your
data, there is no clear answer to "what format is this."

If you know the file will always contain values separated by spaces,
then you should just configure the reader yourself, instead of relying
on the Sniffer. I would never want to use something that "guesses"
the format if I know what it should look like.

···

On May 25, 6:42 pm, Carlos "Guâno" Grohmann <carlos.grohm...@gmail.com> wrote:

I'm using the csv module to read text files, but when I try to open a
single-line file, I can't retrieve any data.
...
# Check for file format with sniffer.
dialect = csv.Sniffer().sniff(sample)
csvfile = csv.reader( csvfile, dialect )

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Carlos Guâno Grohmann wrote:

Hello all

I'm using the csv module to read text files, but when I try to open a
single-line file, I can't retrieve any data.

I'm using this:

            sample = csvfile.read( 1024 )
            csvfile.seek( 0 )

            # Check for file format with sniffer.
            dialect = csv.Sniffer().sniff(sample)
            csvfile = csv.reader( csvfile, dialect )
            # Grab a sample and see if there is a header.
            if csv.Sniffer().has_header( sample ): #if there is a header
                print 'File has header!'
                colnames = csvfile.next() # label columns from first line
                datalist = list( csvfile ) # append data to a list
            else: #if there is NO header
                print 'NO header!'
                datalist = list( csvfile ) # append data to a list
                colnames = ['col_%i' % i for i in
range(len(datalist[0]))] # label columns as col_1, col_2, etc

If I have a single line file like:

58 67

and open it, I get the msg saying the file HAS header, and no data at
all. It work if I add a real header, but that wasn't the idea.

anyone experienced this before?

TIA

Carlos

I'm confused...what does this have to do with wxPython? And how did you get the nickname "Guano"? I don't think I would let people call me that as the common meaning is bird poop...

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Carlos Guâno Grohmann wrote:

I'm using the csv module to read text files, but when I try to open a
single-line file, I can't retrieve any data.

This is OT for this list, you may want to try comp.lang.python. But anyway:

            sample = csvfile.read( 1024 )
            csvfile.seek( 0 )

            # Check for file format with sniffer.
            dialect = csv.Sniffer().sniff(sample)
            csvfile = csv.reader( csvfile, dialect )
            # Grab a sample and see if there is a header.
            if csv.Sniffer().has_header( sample ): #if there is a header

I think this is your problem -- do you need to use the sniffer? Do you need to use it to determine if there is a header, or do you know that there won't be? I suspect it's getting confused by only one line of data -- how can it know if that is a header with no data, or data with no header?

If you do want to sniff the file, I suppose you could kludge around it by special casing one line files...

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Thanks Tim,

I want the app to open data files in different formats, separated by
space, tab, comma, two spaces.. Currently it does read it right if
there is only on separator, but not if there are two or more..
maybe I should look for a different tool to open my files?

cheers

Carlos

···

On May 26, 2:38 pm, Tim Roberts <t...@probo.com> wrote:

On May 25, 6:42 pm, Carlos "Guâno" Grohmann > > <carlos.grohm...@gmail.com> wrote:

> I'm using the csv module to read text files, but when I try to open a
> single-line file, I can't retrieve any data.
>...
> # Check for file format with sniffer.
> dialect = csv.Sniffer().sniff(sample)
> csvfile = csv.reader( csvfile, dialect )

The "Sniffer" uses heuristics to try to guess your format. It is not
omniscient. In particular, if you don't give it enough information to
figure out the format, it has to guess. In your case, given your
data, there is no clear answer to "what format is this."

If you know the file will always contain values separated by spaces,
then you should just configure the reader yourself, instead of relying
on the Sniffer. I would never want to use something that "guesses"
the format if I know what it should look like.
--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.

A good tool -- with support to wxPython -- for dealing with delimited
separated values is Python-DSV: http://python-dsv.sourceforge.net/

Hope this helps.

Cheers,

···

2009/5/26 Carlos Grohmann <carlos.grohmann@gmail.com>:

I want the app to open data files in different formats, separated by
space, tab, comma, two spaces.. Currently it does read it right if
there is only on separator, but not if there are two or more..
maybe I should look for a different tool to open my files?

--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: maurobio@gmail.com
Web: http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."

Thanks Mauro, I'll give it a try

Carlos

···

On May 27, 7:27 am, Mauro Cavalcanti <mauro...@gmail.com> wrote:

2009/5/26 Carlos Grohmann <carlos.grohm...@gmail.com>:

> I want the app to open data files in different formats, separated by
> space, tab, comma, two spaces.. Currently it does read it right if
> there is only on separator, but not if there are two or more..
> maybe I should look for a different tool to open my files?

A good tool -- with support to wxPython -- for dealing with delimited
separated values is Python-DSV:http://python-dsv.sourceforge.net/

Hope this helps.

Cheers,

--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: mauro...@gmail.com
Web:http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."