使用python3 numpy从txt文件加载浮点数(Loading floating numbers from txt file with python3 numpy)

编程入门 行业动态 更新时间:2024-10-28 20:24:41
使用python3 numpy从txt文件加载浮点数(Loading floating numbers from txt file with python3 numpy)

我目前正在尝试创建一个简单的随机浮点数列表,将其保存在文本文件中,然后使用numpy.loadtxt加载带有numpy的浮点数列表。

出于某种原因,每当使用loadtxt它都会声明该文件为空。

/usr/local/lib/python3.4/dist-packages/numpy/lib/npyio.py:891: UserWarning: loadtxt: Empty input file: "mydata1.txt" warnings.warn('loadtxt: Empty input file: "%s"' % fname) [ ] class 'numpy.ndarray'

以下是代码示例:

import numpy import scipy import matplotlib from random import random import codecs floats = list(random() for i in range (10)) fp = open('mydata1.txt','w') for item in floats: str_item="{0:.5f}".format(item) fp.write("%s\n" % str_item ) fp.close floats2 = numpy.loadtxt("mydata1.txt",dtype="str", delimiter="\n") for myfloat in floats2: print("my floats is:",myfloat) print(floats2) print(type(floats2))

I am currently attempting to create a simple list of random floating numbers, save it in a text file and then loading the list of floating numbers with numpy using numpy.loadtxt.

For some reason whenever using loadtxt it states that the file is empty.

/usr/local/lib/python3.4/dist-packages/numpy/lib/npyio.py:891: UserWarning: loadtxt: Empty input file: "mydata1.txt" warnings.warn('loadtxt: Empty input file: "%s"' % fname) [ ] class 'numpy.ndarray'

Here is a sample of the code:

import numpy import scipy import matplotlib from random import random import codecs floats = list(random() for i in range (10)) fp = open('mydata1.txt','w') for item in floats: str_item="{0:.5f}".format(item) fp.write("%s\n" % str_item ) fp.close floats2 = numpy.loadtxt("mydata1.txt",dtype="str", delimiter="\n") for myfloat in floats2: print("my floats is:",myfloat) print(floats2) print(type(floats2))

最满意答案

更改:

fp = open('mydata1.txt','w') for item in floats: str_item="{0:.5f}".format(item) fp.write("%s\n" % str_item ) fp.close

成:

with open('mydata1.txt','w') as fp: for item in floats: fp.write("{0:.5f}\n".format(item))

它应该工作。

with open()打开文件,并在您提交代码后立即将其关闭。 您的fp.close不会关闭该文件,因为您缺少括号。 它应该是: fp.close() 。 默认情况下,文件以缓冲模式打开。 因此,如果您不刷新或关闭文件,则不会将所有数据写入您的文件。

Change:

fp = open('mydata1.txt','w') for item in floats: str_item="{0:.5f}".format(item) fp.write("%s\n" % str_item ) fp.close

into:

with open('mydata1.txt','w') as fp: for item in floats: fp.write("{0:.5f}\n".format(item))

and it should work.

The with open() opens the file and closes it as soon as you dedent your code. Your fp.close does not close the file because your are missing the parenthesis. It should be: fp.close(). Per default, files are opened in buffered mode. So, if you don't flush or close the file no or not all data gets written to your file.

更多推荐

本文发布于:2023-08-03 11:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1386870.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   文件   浮点数   txt   numpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!