On Sun, Feb 1, 2009 at 9:24 AM, vsoler <vicente.so...@gmail.com> wrote:
Hi,

My foo.txt file contains the following:

1,"house","2,5"
2,"table","6,7"
3,"chair","-4,5"

... as seen with notepad.

This file was created with the OpenOffice Calc spreadsheet, but since
I use comma as the decimal separator for numbers, the last value in
each line appears sorrounded by quotes.

I would like to obtain:

[[1,"house",2.5], [2,"table",6.7], [3,"chair",-4.5]]

If I read your requirements, right, I think you want:

import csv

data = "">
reader = csv.reader(open("filename", "r"))
for line in reader:
     data.append(
         line[0], line[1], float(line[2].replace(",", "."))
     )

print data

Although you may want to replace that last bit with
    decimal.Decimal(line[2].replace(",","."))

If you want an exact result and not the approximate floating point result.

Basically the csv module can read through the CSV file very easily, but because you're using commas instead of points you just have to edit that out before you convert it to a number.

--S

Attachment: signature.asc
Description: OpenPGP digital signature

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to