Dan1
Dan1

Reputation: 11

python list of floats from text file

I am trying to create a list of floats from a text file with this code:

exam_data_file = open('exam_data.txt','r')
exam_data = exam_data_file.readlines()
exam_data1 = []

for line1 in exam_data_file:
    line1 = float(line1)
    exam_data1.append(line1)

 print(exam_data1)     

but the output is simply: []

Can someone please help?!

Now I get this error message regardless of the changes I make:

line1 = float(line1)


ValueError: invalid literal for float(): 3.141592654
2.718281828
1.414213562
0.707106781
0.017453293

Upvotes: 0

Views: 3889

Answers (6)

novice programmer
novice programmer

Reputation: 138

https://stackoverflow.com/a/59717679/10151945 This answer is quit relevant. here an example that i have done with :

from ast import literal_eval
file=open("student_list.txt",'r')
for student in file:
    student_list_final = literal_eval(student)
print(student_list_final)
file.close()

Upvotes: 2

Ayemun Hossain Ashik
Ayemun Hossain Ashik

Reputation: 510

Why don't use literal_eval n -(easier to use)

You can read and print a simple list or multi-dimensional lists simply with literal_eval

from ast import literal_eval

f=open("demofile.txt",'r')

for line in f:
    new_list = literal_eval(line)

print(new_list)
f.close()

Upvotes: 2

aldeb
aldeb

Reputation: 6828

There 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 second problem is that line1 is currently a whole line in your file (in your case a chain of separate floats), and not a single float as you expected it to be. That's why you get the Invalid literal error. You have to split it into separate numbers in order to call float upon them. That's why you have to call the string's split method.

Try this instead:

exam_data_file = open('exam_data.txt','r')
exam_data1 = []

for line in exam_data_file:
    exam_data1.extend([float(i) for i in line.split()])

print(exam_data1) 

Output:

[3.141592654, 2.718281828, 1.414213562, 0.707106781, 0.017453293]

Upvotes: 0

Akhil Thayyil
Akhil Thayyil

Reputation: 9403

for line1 in exam_data_file:

should be this :

for line1 in exam_data :

you are referring to a wrong object

Upvotes: 1

pzp
pzp

Reputation: 6597

Since a file-like object is an iterable, you can just use map.

with open('exam_data.txt','r') as f:
    exam_data = map(float, f)

Upvotes: 0

Matt Davidson
Matt Davidson

Reputation: 738

You've made an error choosing the error to iterate over in the for loop.

for line1 in exam_data: # not the file!
    line1 = float(line1)
    exam_data1.append(line1)

This could be further improved with

exam_data = []
with open('exam_data.txt','r') as open_file:
    for line1 in open_file:
        line1 = float(line1)
        exam_data.append(line1)

print(exam_data)

The context handler (with syntax) will make sure you close the file after you have processed it!

Upvotes: 0

Related Questions