last day (15 days later) » 

6:01 PM
0
A: python list of floats from text file

ControllThere are actually two problems in your code: The first problem is that you are actually reading the file two times. One time line 2 (exam_data_file.readlines()) and one second time line 5 while executing the for-loop. You can't read a file twice. For further information see this post. The seco...

 
@Dan1: Could you copy paste the full traceback please?
@Dan1: See my edited post :) I will now write explanations of the changes I made
 
@Controll, i'm not following your reasoning. you say, "The second problem is that line1 is currently a string . . ." but that alone is not a problem. strings can be cast as floats: e.g., float('1.2') returns 1.2. can you rephrase?
 
@dbliss You are absolutely right. see my edited post :)
@dbliss: But the traceback itself says all the information is in one single line. See the third comment on this post.
 
i think that's a consequence of it being in a comment -- comments don't show new lines.
 
@dbliss: I understand. But that's not the point. The message "invalid literal for float(): .... " Means the whole literal quoted in the exception was passed as and argument to float"
 
6:01 PM
oh, totally agree. but that raises a new question. why does a single element in readlines() contain multiple lines? (as opposed to, as you say, "a whole line.")
ok, what do you got for me?
i don't like for line1 in exam_data_file: because it's not for each line1, it's for each line
(what i just said might not be clear, because the code formatting doesn't show up that clearly)
 
Edited :) you're right i just copy pasted his code and kept the variable name
 
word
 
I think the major problem is that the OP wasn't clear about the format of is file
 
yeah
so what are you thinking? does the file have all the numbers on the same line?
 
That's all :)
 
6:05 PM
in the traceback in his question, it looks like it's one number per line, which makes this all very mysterious to me.
 
I think the file has several lines and many number on each line
 
ha
ok
 
else I didn't need a for-loop
 
right
well, i don't know that we're going to get a clear answer from Dan1 about what his file really looks like
so i suppose we should leave it here
 
Have a good day :)
 
6:07 PM
you too
 
:p
 

last day (15 days later) »