On 1 feb, 19:02, Stephen Hansen <apt.shan...@gmail.com> wrote:
> 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
>
>  signature.asc
> < 1 KBVerDescargar

with small modifications, your answers work perfectly!!!

r: in the open statement, why do you use 'rb' as 2nd argument? b is
supposed to be binary, and my file is text!
Steve: your idea works
Stephen: I got an error message saying that append can only take one
argument while you add three; I have added [ ] around the three
arguments; now it's fine; I've also added int() around first argument
to turn it into an integer.

Thank you all indeed!
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to