【问题标题】:Read csv with tab delimiter produces errors使用制表符分隔符读取 csv 会产生错误
【发布时间】:2016-04-22 03:38:47
【问题描述】:

我有一个 CSV 文件,它使用 '\t' TAB 作为分隔符。它包含 5 列。我试过这个:

import numpy as np 
#b=np.loadtxt(r'train_set.csv',dtype=str,delimiter=' ')
my_data = np.genfromtxt('train_set.csv', delimiter='\t')
print my_data

但我收到以下错误:

Traceback (most recent call last):
  File "./wordCloud.py", line 7, in <module>
    my_data = np.genfromtxt('train_set.csv', delimiter='\t')
  File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 1667, in genfromtxt
    raise ValueError(errmsg)
ValueError: Some errors were detected !
    Line #14 (got 4 columns instead of 5)
    Line #21 (got 4 columns instead of 5)
    Line #135 (got 4 columns instead of 5)

有什么想法吗?我不太了解 Python(还 :))!


数据集(我现在也在检查)如下所示:


编辑:

如果我这样做:

my_data = np.genfromtxt('train_set.csv', delimiter='    ')

然后我没有收到任何错误,但输出是:

[ nan  nan  nan ...,  nan  nan  nan]

答案给出了这些警告:

...
    Line #26310 (got 4 columns instead of 5)
    Line #26383 (got 4 columns instead of 5)
    Line #26448 (got 4 columns instead of 5)
    Line #26489 (got 4 columns instead of 5)
    Line #26589 (got 4 columns instead of 5)
    Line #26593 (got 4 columns instead of 5)
    Line #26888 (got 4 columns instead of 5)
    Line #27002 (got 4 columns instead of 5)
    Line #27065 (got 4 columns instead of 5)
    Line #27234 (got 3 columns instead of 5)
    Line #27327 (got 4 columns instead of 5)
    Line #27418 (got 4 columns instead of 5)
    Line #27594 (got 4 columns instead of 5)
    Line #27827 (got 4 columns instead of 5)
    Line #27944 (got 4 columns instead of 5)
    Line #28074 (got 4 columns instead of 5)
    Line #28102 (got 4 columns instead of 5)
    Line #28147 (got 4 columns instead of 5)
    Line #28224 (got 4 columns instead of 5)
    Line #28264 (got 4 columns instead of 5)
    Line #28344 (got 4 columns instead of 5)
    Line #28484 (got 4 columns instead of 5)
  warnings.warn(errmsg, ConversionWarning)

输出会出现一些奇怪的字符,例如:

costing at least \xc2\xa3429

代替costing at least £429

【问题讨论】:

    标签: python numpy io csv


    【解决方案1】:

    您能检查一下 csv 文件的第 14、21 和 135 行吗? 这些行不包含 5 列,因为错误状态(它们都包含 4 列)。

    如果第 5 列应该为空白,只需在末尾插入 \t 字符即可。

    查看您的数据,可能这就是您想要的:

    my_data = np.genfromtxt('train_set.csv', delimiter='\t',
                            invalid_raise=False, skip_header=1,
                            dtype=None)
    

    invalid_raise:这将跳过无效行(#14、21 和 135)。请重新检查它们。 (在 Libre Office 中:使用“另存为”)

    skip_header:名字本身就说明了。

    dtype:应该是None,这样每一列的数据类型就由该列的内容决定了。

    【讨论】:

    • 您提到的行都有第 5 列。我在 Libre Office 中选择了 tab 作为分隔符,现在它显示得很好,所以我可能会说数据集没问题。 (我更新了截图)。
    • Libre Office 在这方面相对宽容。尝试在文本编辑器中打开文件。在 Libre Office 中,转到上述行的空白单元格,在其中输入一些内容,然后删除这些值并保存。然后试试代码。
    • 我也是用gedit打开的,但是一栏有很多字,看不到太多。 Libre Office 不包含空列。
    • 肯定是 CSV 有问题。在 gedit 中,搜索 \t。第 14 行的末尾不应该有任何突出显示,等等 - 这意味着没有 \t
    • 但前几行也是如此,我不知道发生了什么。
    【解决方案2】:

    我也有同样的问题。我的数据是正确的(见下文)但 numpy 报告此类错误:

    Line #11787 (got 4 columns instead of 11)
    Line #11838 (got 3 columns instead of 11)

    我使用 python 加载数据,然后转换为 numpy。所以不是

    tabOryg = numpy.genfromtxt(fn, dtype='str', delimiter='\t')

    我做到了:

        datas = [i.split('\t')  for i in open(fn) ]
        tabOryg = numpy.array(datas, dtype='str')
    

    它有效。我想知道 genfromtxt 有什么问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 2015-12-19
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多