getting values from a text file (newby)

V

vsoler

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]]

in order to process the content of the file. However, so far, I have
not obtained the desired result.

I started testing with (only for first line):

f=open('foo.txt','r')
t=f.readline()
print t
t=t.split(',')
print t
f.close()

But what I am getting is far more complex than what I expected:

1,"house","2,5"

['1', '"house"', '"2', '5"\n']

which is unprocessable.

Can anyboby help?
 
R

r

Try the csv module

py> import csv
py> reader = csv.reader(open(csvfile, "rb"))
py> for row in reader:
print row


['1', 'house', '2,5 ']
['2', 'table', '6,7 ']
['3', 'chair', '-4,5 ']
 
S

Steve Holden

r said:
Try the csv module

py> import csv
py> reader = csv.reader(open(csvfile, "rb"))
py> for row in reader:
print row


['1', 'house', '2,5 ']
['2', 'table', '6,7 ']
['3', 'chair', '-4,5 ']

And then, to conert the last field to numbers? ...

regards
Steve
 
R

r

And then, to conert the last field to numbers? ...

Are you asking me Steve? Well i did not want to short-circuit the OP's
learning process by spoon-feeding him the entire answer. I thought i
would give him a push in the right direction and observe the outcome.

A very intelligent professor once said:
""" It's not about giving a student all the info, only just enough to
spark their own thought process."""
 
S

Steve Holden

Stephen said:
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.
alternatively look at locale.atof() to convert according to what I
presume are the conventions of your locale ...

regards
Steve
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top