简体   繁体   English

来自文本文件的浮点数的python列表

[英]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

Why don't use literal_eval n -(easier to use)为什么不使用literal_eval n -(更容易使用)

You can read and print a simple list or multi-dimensional lists simply with literal_eval您可以简单地使用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()

https://stackoverflow.com/a/59717679/10151945 This answer is quit relevant. https://stackoverflow.com/a/59717679/10151945这个答案是不相关的。 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()

for line1 in exam_data_file:

should be this :应该是这样的:

for line1 in exam_data :

you are referring to a wrong object你指的是错误的对象

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.一个时间线 2 ( exam_data_file.readlines() ) 和第二个时间线 5,同时执行 for 循环。 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.第二个问题是line1当前是您文件中的整行(在您的情况下是一系列单独的浮点数),而不是您预期的单个浮点数。 That's why you get the Invalid literal error.这就是您收到Invalid literal错误的原因。 You have to split it into separate numbers in order to call float upon them.您必须将其拆分为单独的数字,以便对它们调用float That's why you have to call the string'ssplit method.这就是为什么你必须调用字符串的split方法。

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]

You've made an error choosing the error to iterate over in the for loop.您在选择要在 for 循环中迭代的错误时出错。

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!上下文处理程序(带语法)将确保在处理完文件后关闭文件!

Since a file-like object is an iterable, you can just use map .由于类文件对象是可迭代的,因此您可以只使用map

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM