Problem reading csv files

Discussion in 'Python' started by Ramashish Baranwal, Jan 4, 2008.

  1. Hi,

    I am trying to read a csv file using csv.reader. The file is created
    using Open Office and saved in Excel format.

    import csv

    reader = csv.reader(open('test.xls'))
    for row in reader:
    print row

    It however throws the exception _csv.Error:
    <class '_csv.Error'>: line contains NULL byte

    Any idea whats going wrong here?

    Thanks in advance,
    Ram
     
    Ramashish Baranwal, Jan 4, 2008
    #1
    1. Advertising

  2. Ramashish Baranwal

    gsal Guest

    Well, I don't know much python, yet, but I know a csv file when I see
    one...and an Excel files in not a csv file.

    As far as I know, an Excel file is stored probably in binary and in a
    propriatery format...I doubt very much csv.reader would read that just
    like that; then again, I know nothing about cvs.reader.

    A csv file is a comma-separated-file and it is plain text file that
    humans can read with a simple editor...

    I think I would skip that part where you make OpenOffice store the
    file in Excel format and simply save it to plain text.

    Hope this helps.

    gsal
     
    gsal, Jan 4, 2008
    #2
    1. Advertising

  3. Ramashish Baranwal

    Chris Guest

    On Jan 4, 6:24 am, Ramashish Baranwal <>
    wrote:
    > Hi,
    >
    > I am trying to read a csv file using csv.reader. The file is created
    > using Open Office and saved in Excel format.
    >
    > import csv
    >
    > reader = csv.reader(open('test.xls'))
    > for row in reader:
    > print row
    >
    > It however throws the exception _csv.Error:
    > <class '_csv.Error'>: line contains NULL byte
    >
    > Any idea whats going wrong here?
    >
    > Thanks in advance,
    > Ram


    XLS != CSV
    XLS is M$'s format for spreadsheets whereas CSV is essentially a text
    document with comma-delimited fields. If you open it up in OpenOffice
    and go File -> Save As then in the 'Save as type:' drop-down list
    select 'Text CSV (.csv)' and ofc change your code to point to the new
    file.

    If you want to retain it in XLS Format and rather parse that, take a
    look at 'xlrd' and 'pyExcelerator'
     
    Chris, Jan 4, 2008
    #3
  4. >
    > XLS != CSV
    > XLS is M$'s format for spreadsheets whereas CSV is essentially a text
    > document with comma-delimited fields. If you open it up in OpenOffice
    > and go File -> Save As then in the 'Save as type:' drop-down list
    > select 'Text CSV (.csv)' and ofc change your code to point to the new
    > file.
    >
    > If you want to retain it in XLS Format and rather parse that, take a
    > look at 'xlrd' and 'pyExcelerator'


    Thanks for the reply. I made the mistake of assuming XLS = CSV :)

    Ram
     
    Ramashish Baranwal, Jan 5, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray
    Replies:
    0
    Views:
    2,859
  2. hilz

    reading csv files

    hilz, Mar 24, 2006, in forum: Java
    Replies:
    2
    Views:
    6,253
    Roedy Green
    Mar 24, 2006
  3. Michal Mikolajczyk
    Replies:
    0
    Views:
    694
    Michal Mikolajczyk
    Feb 13, 2004
  4. Skip Montanaro
    Replies:
    0
    Views:
    766
    Skip Montanaro
    Feb 13, 2004
  5. Andreas Leitgeb

    Reading CSV-type files...

    Andreas Leitgeb, Jan 17, 2007, in forum: Java
    Replies:
    7
    Views:
    711
    Andreas Leitgeb
    Jan 18, 2007
Loading...

Share This Page