【问题标题】:using numpy to import multiple text files使用 numpy 导入多个文本文件
【发布时间】:2014-04-20 05:59:29
【问题描述】:

我一直在导入多个 txt 文件并使用它们来创建绘图。代码与以前相同,但这次似乎不起作用。我已经把它带回了基础,我不知道出了什么问题。

import numpy    
close('all')    
data = []
pixels = []

for i in range(0,92):    
    data.append(genfromtxt('filename_'+str(i+1)+'.txt', usecols=4))
    pixels.append(genfromtxt('filename_'+str(i+1)+'.txt', usecols=5))

我只需要循环中说明的列,因为 txt 文件有多个值。 这返回:

    raise ValueError(errmsg)
ValueError: Some errors were detected !
    Line #1 (got 2 columns instead of 1)
    Line #3 (got 1 columns instead of 1)
    Line #5 (got 3 columns instead of 1)
    Line #6 (got 3 columns instead of 1)
    Line #8 (got 4 columns instead of 1)
    Line #10 (got 2 columns instead of 1)
    Line #11 (got 2 columns instead of 1)
    Line #12 (got 1 columns instead of 1)
    Line #35 (got 1 columns instead of 1)

任何帮助,都会很棒!

【问题讨论】:

  • 检查您的 txt 文件。看起来问题可能出在他们身上
  • 谢谢。原来我设法破坏了其中一个文件..

标签: python file-io numpy genfromtxt


【解决方案1】:

问题在于您如何传递usecols 参数,它必须是一个序列(例如listtuple),0 是第一列。也许你想要这个:

for i in range(0,92):    
    data.append(genfromtxt('filename_'+str(i+1)+'.txt', usecols=(0,1,2,3)))
    pixels.append(genfromtxt('filename_'+str(i+1)+'.txt', usecols=(0,1,2,3,4)))

【讨论】:

    猜你喜欢
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    相关资源
    最近更新 更多