On 2010-06-03, Carlos Grohmann <carlos.grohm...@gmail.com> wrote:
> Hi all, I'm using csv to read text files, and its working fine, except
> in two cases:
>
> - when there is only one line of text (data) in the file
> - when there is a blank line after the last data line
>
> this is the kind of data:
>
> 45 67 89
> 23 45 06
> 12 34 67
> ...

That data doesn't appear to be csv worthy. Why not use str.split
or str.partition?

> and this is the function:
>
> def getData(paths):
>     """get data from file and create lists with values and column
> names."""
>
>     filehandle = paths#[i] # filehandle = os.path.join(dirname,
> filename)
>     csvfile = open(filehandle,'r') # Open the file and read the

In Python 2.6 and earlier, you need to open the file in binary
mode.

In Python 3.0 and later, you need to open the file with a mystic
incantation:

   csvfile = open(filehandle, newline='')

>     sample = csvfile.read( 1024 )# Grab a sample
>     csvfile.seek( 0 )
>     dialect = csv.Sniffer().sniff(sample) # Check for file format with
> sniffer.
>     csvfile = csv.reader( csvfile, dialect )

Use:

   csvfile = csv.reader(csvfile, dialect=dialect)

dialect is a keyword argument.

>     if csv.Sniffer().has_header( sample ): #if there is a header
>         colnames = csvfile.next() # label columns from first line
>         datalist = list( csvfile ) # append data to a list

Do you really need to use the Sniffer? You'll probably be better
off

-- 
Neil Cerutti
*** You found a dead moose-rat. You sell the hide for $200. ***
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to