2013-11-25 81 views
0

我想創建一個.arff文件,該文件顯示來自我的python代碼的10個最有用的單詞。格式應該是這樣的。試圖使用python創建.arff文件

@attribute pattern1 {yes,no} 
@attribute pattern2 {yes,no} 
...... 
....... 
@attribute emotion {angry,disgusted,fearful,happy,sad,surprised} 

@data 
yes, no, no,......, yes, happy 
no, no, no,....., no, angry 
yes, yes, no,......, yes, sad 

每行應包含10個「真」或「假」值的列表,然後是情感。

這是我迄今爲止所寫的內容,但並未按要求顯示。請幫助我。

f = open("emotions.txt", "w") 
f.write('''@RELATION Emotions\n 
    @ATTRIBUTE word{yes,no} 
    @ATTRIBUTE class {angry,sad,happy,surprised,fearful,disgusted} 
    @DATA\n''') 
for word in varall: 
f.write("%s\n" %word) 
f.close() 
+0

是如何在你的代碼中定義'varall'? – jwodder

+0

作爲列表varall = [] – user2998991

+0

有人可以幫我這個朋友嗎? – user2998991

回答

0

你應該看看this library 它的目的只是爲了這個問題,因爲你的handcoding ARFF輸出是不是一個好主意。

爲了您的屬性,你會做這樣的事情:

arff_writer = arff.Writer(fileName, relation='Emotions', header_names=['pattern1','pattern2', ... 'emotion') 
arff_writer.pytypes[arff.nominal] = '{angry,disgusted,fearful,happy,sad,surprised}' 
arff_writer.write([arff.nominal('emotion')]) 

併爲您的數據:

data = [[1,2,'a'], [3, 4, 'john']] 
arff.dump(open(fileName, 'w'), data, relation="whatever", header_names)