Conversion between Arff file and TXT file _python

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.