Python How to convert file data into float data -
i'm trying convert data inside file read float numbers. large file. won't let me convert because says list. how fix this?:
file = open(filename,'r') line in file: data = file.readlines() line in data: numdata = float(data) file.close()
for line in data: numdata = float(data)
that's problem is. you're calling float()
on list data
instead of on element line
.
incidentally, seem looping twice on file when don't have to. declare file
, iterate on it, , each iteration, you're rereading -- , iterating on again. should more this:
with open(filename, 'r') file: line in file: numdata = float(line)
of course, doubt mean set numdata
(which should called num_data
according pep8, iirc) every time. don't know it's meant used as, though, because variable name unclear, can't tell should instead. if it's list, mean this:
with open(filename, 'r') file: line in file: num_data.append(float(line))
note changed numdata
num_data
.
Comments
Post a Comment