On GitHub, older generations have provided Python libraries for the conversion of files between the two formats, such as Liac-arff:https://github.com/renatopp/liac-arff. However, when the program is more complex, it is cumbersome to call so many external files, and these Arff libraries, when the attribute and the number of values inconsistent, will be error. So, with the support of my brother, I have written two simple conversion functions with reference to overflow. (Spents 5 hours ... To be efficient later.
Arff2txt ():
Convert Arff files to TXT format:
ImportReImportSYSdefarff2txt (filename): txtfile= Open ('./generatedtxt.txt','W') Arr=[] Lines=[] Arff_file=open (filename) forLineinchArff_file:if not(Line.startswith ("@")): if not(Line.startswith ("%")): Line= Line.strip ("\ n") Line= Line.split (',') Arr.append (line)delArr[0] forChildincharr:delChild[10] ifCHILD[9] = ="True": child[9] = 1Else: child[9] =0 Lines.append ('\ t'. Join (Map (str,child))) result='\ n'. Join (lines)Printresult Txtfile.writelines (result) txtfile.close ()
Txt2arff ():
Convert txt files to Arff () format:
defTxt2arff (filename, value): With open ('./generatedarff.arff','W') as Fp:fp.write (" "@relation Exceptionrelation@attribute ID string@attribute thrown Numeric@attribute setlogicflag Numeric@attribute Return numeric@attribute LOC numeric@attribute nummethod numeric@attribute emptyblock numeric@attribute RecoverFlag Numeric@attribute otheroperation numeric@attribute Class-att {true,false} @data" ") with open (filename) as f:contents=F.readlines () forContentinchContents:lines= Content.split ('\ t') Lines= [Line.strip () forLineinchLines]ifLINES[9] = ='1': lines[9] ="True"Lines.append ('{'+ str (value) +'}') Else: lines[9] ="False"Lines.append ('{1}') Array=','. Join (lines) Fp.write ("%s\n"% array)