2014-02-16 38 views
4

这里有一个简单的代码在本link读了一个Python ARFF文件(评论一个没有工作过):导入错误:没有模块名为ARFF

import arff 
for row in arff.load('heart_train.arff'): 
     print(row.sex) 

而这里的错误我收到:

python id3.py 
Traceback (most recent call last): 
    File "id3.py", line 1, in <module> 
    import arff 
ImportError: No module named arff 

“heart_train” ARFF文件中的数据是这样的:

@relation cleveland-14-heart-disease 
@attribute 'age' real 
@attribute 'sex' { female, male} 
@attribute 'cp' { typ_angina, asympt, non_anginal, atyp_angina} 
@attribute 'trestbps' real 
@attribute 'chol' real 
@attribute 'fbs' { t, f} 
@attribute 'restecg' { left_vent_hyper, normal, st_t_wave_abnormality} 
@attribute 'thalach' real 
@attribute 'exang' { no, yes} 
@attribute 'oldpeak' real 
@attribute 'slope' { up, flat, down} 
@attribute 'ca' real 
@attribute 'thal' { fixed_defect, normal, reversable_defect} 
@attribute 'class' { negative, positive} 
@data 
63,male,typ_angina,145,233,t,left_vent_hyper,150,no,2.3,down,0,fixed_defect,negative 
37,male,non_anginal,130,250,f,normal,187,no,3.5,down,0,normal,negative 
41,female,atyp_angina,130,204,f,left_vent_hyper,172,no,1.4,up,0,normal,negative 
56,male,atyp_angina,120,236,f,normal,178,no,0.8,up,0,normal,negative 
57,female,asympt,120,354,f,normal,163,yes,0.6,up,0,normal,negative 
57,male,asympt,140,192,f,normal,148,no,0.4,flat,0,fixed_defect,negative 
... 
+0

你是否也有这一行代码:'进口arff'? – ndpu

+0

是的,我有。它只是没有被复制。 –

+0

其实它是在问题,但不显示!!!?! –

回答

6

您应该重命名你的脚本例如,与其他东西不同,arfftest.py。 Python不能导入arff模块,因为它的名称与您的应用程序文件名相同。我更改之后,ARFF文件,它的工作原理

pip install arff 
# or easy_install arff 
# or pypm install arff 
+0

'莫纳斯-的MacBook-PRO:蒙娜丽莎$蟒蛇id3.py 回溯(最近通话最后一个): 文件 “id3.py”,1号线,在 进口ARFF 导入错误:没有模块名为arff' –

+0

@MonaJalal那个错误意味着python无法找到arff。你安装了它吗? – ndpu

+0

谢谢我使用'pypm install arff'安装了它我收到此错误:'python id3.py Traceback(最近调用最后一次): 文件“id3.py”,第3行,在 print(row.sex ) 文件“/Users/mona/Library/Python/2.7/lib/python/site-packages/arff/__init__.py”,第102行,在__getattr__中 返回对象.__ getattr __(self,key) AttributeError:type object 'object'没有属性'__getattr __'' –

2

NB:

如果你还没有安装arff包本身,与画中画或使用easy_install安装我删除单代码来自属性的第二列。

这里是ARFF文件:

@relation cleveland-14-heart-disease 
@attribute age real 
@attribute sex { female, male} 
@attribute cp { typ_angina, asympt, non_anginal, atyp_angina} 
@attribute trestbps real 
@attribute chol real 
@attribute fbs { t, f} 
@attribute restecg { left_vent_hyper, normal, st_t_wave_abnormality} 
@attribute thalach real 
@attribute exang { no, yes} 
@attribute oldpeak real 
@attribute slope { up, flat, down} 
@attribute ca real 
@attribute thal { fixed_defect, normal, reversable_defect} 
@attribute class { negative, positive} 
@data 
63,male,typ_angina,145,233,t,left_vent_hyper,150,no,2.3,down,0,fixed_defect,negative 
37,male,non_anginal,130,250,f,normal,187,no,3.5,down,0,normal,negative 
41,female,atyp_angina,130,204,f,left_vent_hyper,172,no,1.4,up,0,normal,negative 
56,male,atyp_angina,120,236,f,normal,178,no,0.8,up,0,normal,negative 
57,female,asympt,120,354,f,normal,163,yes,0.6,up,0,normal,negative 
57,male,asympt,140,192,f,normal,148,no,0.4,flat,0,fixed_defect,negative 

片段和输出:

:/tmp:~ cat a.py 
import arff 
for row in arff.load('heart_train.arff'): 
    print(row.sex) 
:/tmp:~ python a.py 
male 
male 
female 
male 
female 
male 
:/tmp:~ 
male 
:/tmp:~ 
相关问题