python - 运行目录中的文件会创建一个额外的 nan 数组

标签 python numpy operating-system genfromtxt

我正在使用以下 for 循环,该循环在一个目录上运行,该目录包含一些带有数字(节点)列表的文件。

for filename in os.listdir(nodes_path):
    nodes_arr = np.genfromtxt(os.path.join(nodes_path, filename), delimiter=',')
    print (nodes_arr)
    print (len(nodes_arr))

在此示例中,目录中有 4 个文件。 我已在 nodes_path 中设置了它的路径。 问题是我得到了包含 nan 的第五个字符串。 输出:

[22718045.  1172884.  1434416. ... 84287109. 84328117. 84381796.]
219463
[6.9590000e+03 7.7180000e+03 1.3373000e+04 ... 8.3828038e+07 8.4007117e+07
 8.4046414e+07]
63309
[56467551. 12073526. 12655638. ... 84299018. 84312857. 84388811.]
271456
[81614683. 11760788. 15679375. ... 84431649. 84448307. 84458895.]
274465
[nan nan nan nan]
4

我不明白为什么会发生这种情况。

最佳答案

找到的解决方案: 从 for 循环中排除最后一个文件,如下所示:

for filename in os.listdir(nodes_path)[:-1]:
DO ...

关于python - 运行目录中的文件会创建一个额外的 nan 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59610938/

相关文章:

python - 这些频谱波段以前是靠肉眼判断的,怎么用程序来判断呢?

python - 类型错误 : 'str' object is not callable?

python - 如何检查 Pandas 系列中的所有元素是否等于特定值

c++ - 一个程序会分配多少栈内存

python - 如何在模板中迭代多维列表/字典

python - Django UUID 字段抛出 IntegrityError

python - 从 NumPy 或 PyTorch 中的矩阵获取对角线 "stripe"

python - 具有灰色世界假设的自动白平衡

assembly - iretq 抛出 GP 错误

c - 当我堆栈溢出时,操作系统如何避免崩溃?