LoginSignup
0
1

More than 3 years have passed since last update.

テキストファイルをいい感じにwekaで使えるarffに変換したい

Posted at

はじめに

みなさんご存知、機械学習のソフトwekaはarffファイルというweka独自のファイルを使います。
なので、テキストファイルからarffに自動で変換するやつを作りました。

arffファイルを作る

import os

#実行中のディレクトリの場所
path = os.path.dirname(__file__)+'作りたいarffの名前.arff'

#実行中のディレクトリの場所にarffファイルを新規作成
with open(path, 'w') as f1:
    f1.write('@reration accepted\n\n@attribute 属性の名前1 属性の型\n@attribute 属性の名前2 {属性の要素1,属性の要素2}\n\n@data\n')

#テキストファイルを読み込んで
fp = open('読みたいテキストファイル', 'r')
#for文でテキストファイルを一行ずつ処理
for line in fp.readlines():
    #さっき作ったarffに追記
    with open(os.path.join(path,filename + '.arff'), 'a') as f1:
        f1.write('\''+line[:-1]+'\',accepted\n')
    line = fp.readlines()
fp.close()
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1