-
Notifications
You must be signed in to change notification settings - Fork 46
/
arffToCsv.py
46 lines (41 loc) · 1.49 KB
/
arffToCsv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#########################################
# Project : ARFF to CSV converter #
# Created : 10/01/17 11:08:06 #
# Author : haloboy777 #
# Licence : MIT #
#########################################
# Importing library
import os
# Getting all the arff files from the current directory
files = [arff for arff in os.listdir('.') if arff.endswith(".arff")]
# Function for converting arff list to csv list
def toCsv(text):
data = False
header = ""
new_content = []
for line in text:
if not data:
if "@ATTRIBUTE" in line or "@attribute" in line:
attributes = line.split()
if("@attribute" in line):
attri_case = "@attribute"
else:
attri_case = "@ATTRIBUTE"
column_name = attributes[attributes.index(attri_case) + 1]
header = header + column_name + ","
elif "@DATA" in line or "@data" in line:
data = True
header = header[:-1]
header += '\n'
new_content.append(header)
else:
new_content.append(line)
return new_content
# Main loop for reading and writing files
for file in files:
with open(file, "r") as inFile:
content = inFile.readlines()
name, ext = os.path.splitext(inFile.name)
new = toCsv(content)
with open(name + ".csv", "w") as outFile:
outFile.writelines(new)