简体   繁体   English

使用numpy.loadtxt,如何将.txt文件中的字符串转换为整数值/浮点数?

[英]Using numpy.loadtxt, how does one convert strings in the .txt file into integer values/floats?

So, I have a .txt file that I want to read into Pylab. 因此,我有一个.txt文件,我想读入Pylab。 The problem is that, when I try to do so using the numpy.loadtxt("filename.txt") , Pylab cannot read the numbers in the array in my file as float values (it returns the error: cannot convert string to float .). 问题是,当我尝试使用numpy.loadtxt("filename.txt") ,Pylab无法读取文件中数组中的数字作为浮点值(它返回错误: cannot convert string to float 。 )。

I am not sure if there is something wrong with my syntax as above; 我不确定上述语法是否有问题; when I remove the quotation marks inside the parentheses, numpy.loadtxt(filename.txt) , Pylab returns the error: filename is not defined. 当我删除括号numpy.loadtxt(filename.txt)内的引号时,Pylab返回错误:未定义文件名。

Any suggestions on how to read a series of numbers saved in a .txt file into Pylab as an array of floats? 关于如何将一系列保存在.txt文件中的数字作为浮点数数组读入Pylab的任何建议?

You need to provide sample lines in your filename.txt file. 您需要在filename.txt文件中提供示例行。 I guess you may need to read the doc for numpy.loadtxt here . 我猜您可能需要在这里阅读numpy.loadtxt的文档。 There are some good examples on the document page. 文档页面上有一些很好的示例。

BTW, the second command numpy.loadtxt(filename.txt) is wrong since you have not defined a variable filename . 顺便说一句,第二个命令numpy.loadtxt(filename.txt)是错误的,因为您尚未定义变量filename

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

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