gpt4 book ai didi

python - 防止或消除 loadtxt 中的 'empty file' 警告

转载 作者:太空狗 更新时间:2023-10-29 18:23:13 25 4
gpt4 key购买 nike

我的代码遍历许多文件,使用以下命令将它们读入列表:

data = np.loadtxt(myfile, unpack=True)

其中一些文件是空的(我无法控制),当发生这种情况时,我会在屏幕上打印此警告:

/usr/local/lib/python2.7/dist-packages/numpy/lib/npyio.py:795: UserWarning: loadtxt: Empty input file: "/path_to_file/file.dat"
warnings.warn('loadtxt: Empty input file: "%s"' % fname)

如何防止显示此警告?

最佳答案

您必须用 catch_warnings 换行,然后调用 simplefilter 方法来抑制这些警告。例如:

import warnings

with warnings.catch_warnings():
warnings.simplefilter("ignore")
data = np.loadtxt(myfile, unpack=True)

应该这样做。

关于python - 防止或消除 loadtxt 中的 'empty file' 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19167550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com