I am having some difficulty converting this list of strings into a list of floats. I tried this two ways and each returned different errors.
import csv
import math
unemp_reader = csv.reader(open('unemp.csv', 'rU'))
unemp_lines = list(unemp_reader)
for rows in unemp_lines: #tried this way, but error tells me indices must be integers
i = 1
for i in rows:
a = map(float, unemp_lines[i])
float_list.append(a)
print float_list
for row in unemp_lines: #tried this way but the list returned is empty
y = row[1].split(",")[1:-1]
float_list = [float(i) for i in y if i]
print float_list
Your issue here in your first example is that a
forloop doesn’t give indices, it gives the values from the list. This means that your example doesn’t really make any sense at all.The second example takes the second item in the row, splits it on commas (which should all be dealt with by the
csvmodule anyway) and then takes the second through second to last item of the resulting list. As I imagine there were no commas in the value this will take[1:-1]of a list with one element, returning an empty list. I can’t really understand the intent here. You also then only store the data from the last row (overwritingfloat_listeach time). You appear to be second-guessing thecsvmodule and making this a lot harder than it is.You need to stop overcomplicating it:
To explain, first we use the
withstatement to open the file in a readable and pythonic way (which ensures the file is closed properly, even on exceptions). We then make acsv.readerto get our data from the CSV file. We skip the headers by advancing the iterator by one, meaning we start on the second row. We then use a list comprehension to produce a new list from the iterator, containing another list comprehension generating the floats of the values if those values exist, and are not in theYearorAnnualcolumn. To do this we use theenumerate()builtin to get the number of the column we are in, then do a check to ensure it’s not0(Year) or13(Annual).As J.F.Sebastian points out in the comments, the best solution is to allow the
csvmodule to handle dealing with the numbers for you, by adding the named argumentquotingto thecsv.reader()call with the valuecsv.QUOTE_NONNUMERIC. E.g: