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

标签 python numpy

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

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/

相关文章:

Python 字典抛出无效语法

python - Python 中的拱形关系信息图

python - Python 中二维图像的非均匀采样

numpy - 调用 cv2.rectangle 时无法绘制框

python - 在多处理进程之间共享大型只读 Numpy 数组

python - 在 Keras 中使用 K.eval() 将 Tensor 转换为 np.array 返回 InvalidArgumentError

python - 运行测试时如何让 tox 尊重系统版本?

python - matplotlib:如何获取现有 twinx() 轴的句柄?

python - 比较数据框 python 的两列后找到额外的值

python - 在 Python 中查找 numpy 数组是否是较大数组的子集