Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to transform strings in csv file into float and store them as a list?

I have csv file that looks like this:

id,type,inc,a,b
1,2,63376.799999999996,0.3061,187
2,1,58087.700000000004,0.3361,178
3,1,52699.9,0.3482,181
4,2,72964.8,0.3186,177
5,4,111589.79999999999,0.268,154
6,4,107618.0,0.2583,150
7,2,87109.2,0.3233,193
8,2,84669.59999999999,0.308,179
9,2,77258.4,0.3247,173

I need to transform values from fields [3], [4] and [5] that are ‘inc’, ‘a’ and ‘b’ into float and than append to households to create a list.

Finally it should print len(households)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am trying this

import csv 
import scipy.optimize as opt
def read_households(filename):
    households=[]
    fh = open("households.csv", 'r')
    reader = csv.DictReader(fh) #creates object 'reader' to read the file
    for hh in reader:
        reader[3,4,5] = float(reader[3,4,5])
        households.append(hh)
        
        print("lines read:", len(households))
        return(household)

It does not give me anything.

>Solution :

Try this code for get some output from CSV.

import csv 
import scipy.optimize as opt

def read_households(filename):
    households=[]
    fh = open(filename, 'r')
    reader = csv.DictReader(fh)
    for hh in reader:
        hh['inc'] = float(hh['inc'])
        hh['a'] = float(hh['a'])
        hh['b'] = float(hh['b'])
        households.append(hh)
        
    print("lines read:", len(households))
    return households

# call function
read_households('path_of_csv_file')

Result :

lines read: 9
[{'id': '1', 'type': '2', 'inc': 63376.799999999996, 'a': 0.3061, 'b': 187.0},
 {'id': '2', 'type': '1', 'inc': 58087.700000000004, 'a': 0.3361, 'b': 178.0},
 {'id': '3', 'type': '1', 'inc': 52699.9, 'a': 0.3482, 'b': 181.0},
 {'id': '4', 'type': '2', 'inc': 72964.8, 'a': 0.3186, 'b': 177.0},
 {'id': '5', 'type': '4', 'inc': 111589.79999999999, 'a': 0.268, 'b': 154.0},
 {'id': '6', 'type': '4', 'inc': 107618.0, 'a': 0.2583, 'b': 150.0},
 {'id': '7', 'type': '2', 'inc': 87109.2, 'a': 0.3233, 'b': 193.0},
 {'id': '8', 'type': '2', 'inc': 84669.59999999999, 'a': 0.308, 'b': 179.0},
 {'id': '9', 'type': '2', 'inc': 77258.4, 'a': 0.3247, 'b': 173.0}]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading