import csv
from time import sleep
class convert(object):
content = []
name = ''
def csvInput(self,user):
if user.endswith('.csv') == True:
self.name = user.replace('.csv', '')
print 'Opening CSV file.'
try:
with open(user, 'rb') as csvfile:
lines = csv.reader(csvfile, delimiter = ',')
for row in lines:
self.content.append(row)
csvfile.close()
except IOError:
print 'File not found.\n'
def arffOutput(self):
print 'Converting to ARFF file.\n'
title = str(self.name) + '.arff'
new_file = open(title, 'w')
##
#following portions formats and writes to the new ARFF file
##
#write relation
new_file.write('@relation ' + str(self.name)+ '\n\n')
#get attribute type input
for i in range(len(self.content[0])-1):
new_file.write('@attribute ' + str(self.content[0][i]) + '\n')
#create list for class attribute
last = len(self.content[0])
class_items = []
for i in range(len(self.content)):
name = self.content[i][last-1]
if name not in class_items:
class_items.append(self.content[i][last-1])
else:
pass
del class_items[0]
string = '{' + ','.join(sorted(class_items)) + '}'
new_file.write('@attribute ' + str(self.content[0][last-1]) + ' ' + str(string) + '\n')
#write data
new_file.write('\n@data\n')
del self.content[0]
for row in self.content:
new_file.write(','.join(row) + '\n')
#close file
new_file.close()
#####
run = convert()
run.csvInput
run.arffOutput
aW1wb3J0IGNzdgpmcm9tIHRpbWUgaW1wb3J0IHNsZWVwCgpjbGFzcyBjb252ZXJ0KG9iamVjdCk6CgogICAgY29udGVudCA9IFtdCiAgICBuYW1lID0gJycKICAKICAgIGRlZiBjc3ZJbnB1dChzZWxmLHVzZXIpOgoKICAgICAgICBpZiB1c2VyLmVuZHN3aXRoKCcuY3N2JykgPT0gVHJ1ZToKICAgICAgICAgICAgc2VsZi5uYW1lID0gdXNlci5yZXBsYWNlKCcuY3N2JywgJycpCiAgICAgICAgICAgIAogICAgICAgIHByaW50ICdPcGVuaW5nIENTViBmaWxlLicgICAgIAogICAgICAgIHRyeToKICAgICAgICAgICAgd2l0aCBvcGVuKHVzZXIsICdyYicpIGFzIGNzdmZpbGU6CiAgICAgICAgICAgICAgIGxpbmVzID0gY3N2LnJlYWRlcihjc3ZmaWxlLCBkZWxpbWl0ZXIgPSAnLCcpCiAgICAgICAgICAgICAgIGZvciByb3cgaW4gbGluZXM6CiAgICAgICAgICAgICAgICAgICBzZWxmLmNvbnRlbnQuYXBwZW5kKHJvdykKICAgICAgICAgICAgY3N2ZmlsZS5jbG9zZSgpCiAgICAgICAgZXhjZXB0IElPRXJyb3I6CiAgICAgICAgICAgIHByaW50ICdGaWxlIG5vdCBmb3VuZC5cbicgICAgICAgICAgICAKCiAgICBkZWYgYXJmZk91dHB1dChzZWxmKToKICAgICAgICBwcmludCAnQ29udmVydGluZyB0byBBUkZGIGZpbGUuXG4nCiAgICAgICAgdGl0bGUgPSBzdHIoc2VsZi5uYW1lKSArICcuYXJmZicKICAgICAgICBuZXdfZmlsZSA9IG9wZW4odGl0bGUsICd3JykKCiAgICAgICAgIyMKICAgICAgICAjZm9sbG93aW5nIHBvcnRpb25zIGZvcm1hdHMgYW5kIHdyaXRlcyB0byB0aGUgbmV3IEFSRkYgZmlsZQogICAgICAgICMjCgogICAgICAgICN3cml0ZSByZWxhdGlvbgogICAgICAgIG5ld19maWxlLndyaXRlKCdAcmVsYXRpb24gJyArIHN0cihzZWxmLm5hbWUpKyAnXG5cbicpCgogICAgICAgICNnZXQgYXR0cmlidXRlIHR5cGUgaW5wdXQKICAgICAgICBmb3IgaSBpbiByYW5nZShsZW4oc2VsZi5jb250ZW50WzBdKS0xKToKICAgICAgICAgICAgbmV3X2ZpbGUud3JpdGUoJ0BhdHRyaWJ1dGUgJyArIHN0cihzZWxmLmNvbnRlbnRbMF1baV0pICsgICAnXG4nKQoKICAgICAgICAjY3JlYXRlIGxpc3QgZm9yIGNsYXNzIGF0dHJpYnV0ZQogICAgICAgIGxhc3QgPSBsZW4oc2VsZi5jb250ZW50WzBdKQogICAgICAgIGNsYXNzX2l0ZW1zID0gW10KICAgICAgICBmb3IgaSBpbiByYW5nZShsZW4oc2VsZi5jb250ZW50KSk6CiAgICAgICAgICAgIG5hbWUgPSBzZWxmLmNvbnRlbnRbaV1bbGFzdC0xXQogICAgICAgICAgICBpZiBuYW1lIG5vdCBpbiBjbGFzc19pdGVtczoKICAgICAgICAgICAgICAgIGNsYXNzX2l0ZW1zLmFwcGVuZChzZWxmLmNvbnRlbnRbaV1bbGFzdC0xXSkKICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgIHBhc3MgIAogICAgICAgIGRlbCBjbGFzc19pdGVtc1swXQogICAgCiAgICAgICAgc3RyaW5nID0gJ3snICsgJywnLmpvaW4oc29ydGVkKGNsYXNzX2l0ZW1zKSkgKyAnfScKICAgICAgICBuZXdfZmlsZS53cml0ZSgnQGF0dHJpYnV0ZSAnICsgc3RyKHNlbGYuY29udGVudFswXVtsYXN0LTFdKSArICcgJyArIHN0cihzdHJpbmcpICsgJ1xuJykKCiAgICAgICAgI3dyaXRlIGRhdGEKICAgICAgICBuZXdfZmlsZS53cml0ZSgnXG5AZGF0YVxuJykKCiAgICAgICAgZGVsIHNlbGYuY29udGVudFswXQogICAgICAgIGZvciByb3cgaW4gc2VsZi5jb250ZW50OgogICAgICAgICAgICBuZXdfZmlsZS53cml0ZSgnLCcuam9pbihyb3cpICsgJ1xuJykKCiAgICAgICAgI2Nsb3NlIGZpbGUKICAgICAgICBuZXdfZmlsZS5jbG9zZSgpCgogICAgCiMjIyMjICAgIApydW4gPSBjb252ZXJ0KCkKcnVuLmNzdklucHV0CnJ1bi5hcmZmT3V0cHV0CgoK