My script is picking up a string when it should not, I get
ValueError: could not convert string to float: for line
list_col.append(float(row[c].strip('"')))in the code below
with open(fileTwoName) as f:
reader = csv.reader(f, delimiter=",")
fileTwoColumnNames = next(reader)
output_column .extend(fileTwoColumnNames[2:-1])
number_of_columns = len(fileTwoColumnNames)-2
for c in range(2,number_of_columns+2):
list_col = []
f.seek(1)
next(reader)
for row in reader:
list_col.append(float(row[c].strip('"')))
list_col_name_wise = []
for k in range(0, len(number_eng)):
list_col_name_wise.append(sum(list_col[start_row[k] - 1:start_row[k] - 1 + 1- total_case [k]]))
data.append(list_col_name_wise)
I tested it by adding
if list_col == str:
list_col.append(float(row[c].strip('"'))) `
everything outputs ok but I need my data list to output as a float. Is there a way for me to account for both a str and float?
print row[c].strip('"')before the offending line and the last result before it errors.row[c].strip('""')is a string, which should contain only a float and maybe whitespace, otherwisefloat(...)will fail, with the error you're seeing,