【问题标题】:Cannot import data from txt file无法从txt文件导入数据
【发布时间】:2018-01-10 10:43:33
【问题描述】:

这是我的代码

import numpy as np #Data manupulation import and export data = np.genfromtxt('data_file.txt', delimiter=',') data[0:4]

以下是错误信息。

C:\Users\udari\AppData\Local\Programs\Python\Python36\python.exe "E:/My Works/PythonWorks/myScript.py"
Traceback (most recent call last):
  File "E:/My Works/PythonWorks/myScript.py", line 4, in <module>
    data = np.genfromtxt('data_file.txt', delimiter=',')
  File "C:\Users\udari\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\lib\npyio.py", line 2014, in genfromtxt
    raise ValueError(errmsg)
ValueError: Some errors were detected !

 - Line #2 (got 1 columns instead of 2)
    Line #3 (got 1 columns instead of 2)
    Line #4 (got 1 columns instead of 2)
    Line #5 (got 1 columns instead of 2)
    Line #6 (got 1 columns instead of 2)
    Line #7 (got 1 columns instead of 2)
    Line #8 (got 3 columns instead of 2)
    Line #9 (got 3 columns instead of 2)
    Line #10 (got 5 columns instead of 2)
    Line #11 (got 1 columns instead of 2)
    Line #12 (got 1 columns instead of 2)
    Line #13 (got 3 columns instead of 2)
    Line #14 (got 3 columns instead of 2)
    Line #15 (got 4 columns instead of 2)
    Line #16 (got 3 columns instead of 2)
    Line #17 (got 1 columns instead of 2)
    Line #18 (got 1 columns instead of 2)
    Line #19 (got 1 columns instead of 2)
    Line #21 (got 1 columns instead of 2)
    Line #22 (got 1 columns instead of 2)
    Line #23 (got 1 columns instead of 2)
    Line #24 (got 1 columns instead of 2)
    Line #26 (got 1 columns instead of 2)
    Line #27 (got 7 columns instead of 2)
    Line #28 (got 4 columns instead of 2)
    Line #31 (got 9 columns instead of 2)
    Line #32 (got 1 columns instead of 2)
    Line #33 (got 3 columns instead of 2)
    Line #35 (got 1 columns instead of 2)
    Line #36 (got 1 columns instead of 2)
    Line #37 (got 1 columns instead of 2)
    Line #38 (got 1 columns instead of 2)
    Line #39 (got 1 columns instead of 2)
    Line #40 (got 1 columns instead of 2)
    Line #41 (got 1 columns instead of 2)
    Line #42 (got 1 columns instead of 2)
    Line #43 (got 1 columns instead of 2)
    Line #44 (got 1 columns instead of 2)
    Line #45 (got 1 columns instead of 2)
    Line #46 (got 1 columns instead of 2)
    Line #47 (got 1 columns instead of 2)
    Line #48 (got 1 columns instead of 2)
    Line #49 (got 1 columns instead of 2)
    Line #50 (got 1 columns instead of 2)
    Line #51 (got 1 columns instead of 2)
    Line #52 (got 1 columns instead of 2)
    Line #53 (got 3 columns instead of 2)
    Line #54 (got 1 columns instead of 2)
    Line #55 (got 1 columns instead of 2)
    Line #56 (got 1 columns instead of 2)
    Line #57 (got 3 columns instead of 2)
    Line #58 (got 1 columns instead of 2)
    Line #59 (got 1 columns instead of 2)
    Line #60 (got 1 columns instead of 2)
    Line #61 (got 1 columns instead of 2)
    Line #62 (got 1 columns instead of 2)
    Line #63 (got 1 columns instead of 2)
    Line #64 (got 1 columns instead of 2)
    Line #65 (got 1 columns instead of 2)
    Line #66 (got 1 columns instead of 2)
    Line #67 (got 3 columns instead of 2)
    Line #68 (got 1 columns instead of 2)
    Line #69 (got 1 columns instead of 2)
    Line #71 (got 1 columns instead of 2)
    Line #72 (got 3 columns instead of 2)
    Line #73 (got 1 columns instead of 2)
    Line #74 (got 1 columns instead of 2)
    Line #76 (got 1 columns instead of 2)
    Line #77 (got 3 columns instead of 2)
    Line #78 (got 1 columns instead of 2)
    Line #79 (got 1 columns instead of 2)
    Line #80 (got 1 columns instead of 2)
    Line #81 (got 1 columns instead of 2)
    Line #82 (got 1 columns instead of 2)
    Line #83 (got 1 columns instead of 2)
    Line #84 (got 1 columns instead of 2)
    Line #85 (got 1 columns instead of 2)
    Line #86 (got 1 columns instead of 2)
    Line #87 (got 1 columns instead of 2)
    Line #88 (got 1 columns instead of 2)
    Line #89 (got 1 columns instead of 2)
    Line #90 (got 1 columns instead of 2)
    Line #91 (got 3 columns instead of 2)
    Line #92 (got 1 columns instead of 2)
    Line #93 (got 3 columns instead of 2)
    Line #94 (got 1 columns instead of 2)
    Line #95 (got 1 columns instead of 2)
    Line #97 (got 1 columns instead of 2)
    Line #98 (got 3 columns instead of 2)
    Line #100 (got 3 columns instead of 2)
    Line #102 (got 3 columns instead of 2)
    Line #104 (got 1 columns instead of 2)
    Line #105 (got 17 columns instead of 2)
    Line #106 (got 17 columns instead of 2)
    Line #107 (got 17 columns instead of 2)
    Line #108 (got 17 columns instead of 2)
    Line #109 (got 17 columns instead of 2)
    Line #110 (got 17 columns instead of 2)
    Line #111 (got 17 columns instead of 2)
    Line #112 (got 17 columns instead of 2)
    Line #113 (got 17 columns instead of 2)
    Line #114 (got 17 columns instead of 2)
    Line #115 (got 17 columns instead of 2)
    Line #116 (got 17 columns instead of 2)
    Line #117 (got 17 columns instead of 2)
    Line #118 (got 17 columns instead of 2)
    Line #119 (got 17 columns instead of 2)
    Line #120 (got 17 columns instead of 2)
    Line #121 (got 17 columns instead of 2)
    Line #122 (got 17 columns instead of 2)
    Line #123 (got 17 columns instead of 2)
    Line #124 (got 17 columns instead of 2)
    Line #125 (got 17 columns instead of 2)
    Line #126 (got 17 columns instead of 2)
    Line #127 (got 17 columns instead of 2)
    Line #128 (got 17 columns instead of 2)
    Line #129 (got 17 columns instead of 2)
    Line #130 (got 17 columns instead of 2)
    Line #131 (got 17 columns instead of 2)
    Line #132 (got 17 columns instead of 2)
    Line #133 (got 17 columns instead of 2)
    Line #134 (got 17 columns instead of 2)
    Line #135 (got 17 columns instead of 2)
    Line #136 (got 17 columns instead of 2)
    Line #137 (got 17 columns instead of 2)
    Line #138 (got 17 columns instead of 2)
    Line #139 (got 17 columns instead of 2)
    Line #140 (got 17 columns instead of 2)
    Line #141 (got 17 columns instead of 2)
    Line #142 (got 17 columns instead of 2)
    Line #143 (got 17 columns instead of 2)
    Line #144 (got 17 columns instead of 2)
    Line #145 (got 17 columns instead of 2)
    Line #146 (got 17 columns instead of 2)
    Line #147 (got 17 columns instead of 2)
    Line #148 (got 17 columns instead of 2)
    Line #149 (got 17 columns instead of 2)
    Line #150 (got 17 columns instead of 2)
    Line #151 (got 17 columns instead of 2)
    Line #152 (got 17 columns instead of 2)
    Line #153 (got 17 columns instead of 2)
    Line #154 (got 17 columns instead of 2)
    Line #155 (got 17 columns instead of 2)
    Line #156 (got 17 columns instead of 2)
    Line #157 (got 17 columns instead of 2)
    Line #158 (got 17 columns instead of 2)
    Line #159 (got 17 columns instead of 2)
    Line #160 (got 17 columns instead of 2)
    Line #161 (got 17 columns instead of 2)
    Line #162 (got 1 columns instead of 2)
    Line #163 (got 1 columns instead of 2)
    Line #164 (got 1 columns instead of 2)

Process finished with exit code 1

【问题讨论】:

  • 你的文本文件是什么样的?
  • relation 'labor-neg-data' 属性 'duration' 数值属性 'wage-increase-first-year' 数值属性 'wage-increase-second-year' 数值属性 'wage-increase-third -year' 数字属性 'cost-of-living-adjustment' {'none','tcf','tc'} 属性 'working-hours' 数字
  • , 是您在文本文件中的列分隔符吗?
  • 是的............
  • @data 1,5,?,?,?,40,?,?,2,?,11,'average',?,?,'yes',?,'good' 2 ,4.5,5.8,?,?,35,'ret_allw',?,?,'yes',11,'below_average',?,'full',?,'full','good'

标签: python numpy


【解决方案1】:

从您放入 cmets 的文本文件示例到问题,很明显您的文件是 ARFF filenumpy.genfromtxt 无法读取这样的文件。相反,您可以使用scipy.io.arff.loadarff。但是请注意,SciPy ARFF 阅读器不理解完整的 ARFF 文件格式。如果您的文件使用了未在 SciPy 阅读器中实现的功能,它将无法工作。

在 python 中读取 ARFF 文件的其他选项包括 liac-arffarff

【讨论】:

  • import arff data = arff.load(open('data_file.arff', 'rb')) 这不起作用
  • import scipy data = scipy.io.arff.loadarff(open('data_file.arff', ',')) 这会生成一个错误,因为“AttributeError: module 'scipy' has no attribute 'io '''我该怎么办?
  • 我怀疑你的程序开头只有import scipy。要使用全名scipy.io.arff.loadarff 的函数,必须使用import scipy.io。如果只使用import scipy,子包(如io)不会自动导入。
  • import scipy.io.arff data = scipy.io.arff.loadarff(open('data_file.arff')) data 我写我的代码如上。现在没有错误。但是数据结果没有显示arff文件。帮我显示数据文件中的数据
猜你喜欢
  • 2017-09-30
  • 1970-01-01
  • 2015-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
相关资源
最近更新 更多