Python question about converting values from string to float. I have to write a
ID: 3873265 • Letter: P
Question
Python question about converting values from string to float.
I have to write a function named convert() that takes string, file to integer column. Column is a number that contains the values that need conversion from string to float. I have to strip newline character at the end of the line and need to slice to skip the header.
def convert(file, column):
'''
try converting a column in a file with floating values
from string to a list of float values.
'''
col_values = []
with open(test,'r') as file:
read_lns = file.readlines()
for line in read_lns[1:]:
next(read_lns, None)
col = line.strip(' ').split()
columns = col_values.append(int(col[1]))
column_values = float(cols)
return column_values
return col_values
It should return:
>>> type(convert(file, -2))
>>> float
Explanation / Answer
so,The type(conver(file,-2)) will always return <class 'list'> because you are returning the list values not float values. I am posting below example to clear your confusion.
>>> a = list()
>>> type(a)
<class 'list'>
>>> b = 1.5
>>> type(b)
<class 'float'>
so, your method will return list and it is expected behaviour.
please comment if you have more questions.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.