python實(shí)現(xiàn)txt文件格式轉(zhuǎn)換為arff格式
本文實(shí)例為大家分享了python實(shí)現(xiàn)txt文件格式轉(zhuǎn)換為arff格式的具體代碼,供大家參考,具體內(nèi)容如下
將文件讀取出來(lái)的時(shí)候默認(rèn)都是字符型的,所以有轉(zhuǎn)換出來(lái)有點(diǎn)問(wèn)題,但是還是可以用的。
文件要求第一行是你對(duì)應(yīng)的屬性名,之后是數(shù)字。
import sys import re relationname = "" filename = "" if (len(sys.argv)<2): print("Usage:\npython arff.py MyRelationName filename.txt") else: relationname = sys.argv[1] filename = sys.argv[2] class Arff: def __init__(self, r, f): self.relationname = r if r is not "" else "MachineLearning" f = f if f is not "" else "MMG_data.txt" self.file1 = open(f, 'r') self.data = [] self.names = [] self.parseData() self.writeToFile() def parseData(self): firstLine = True for line in self.file1.readlines(): if not firstLine: try: line = line.replace("\n", "") words = line.split(" ") except ValueError: print("cant parse file!!") self.data.append(words) else: firstLine = False line = line.replace("\n", "") words = line.split(" ") self.names = words def getType(self, value): v = "" if(type(value) == type(1)): v = "numeric" elif(type(value) == type(1.0)): v = "numeric" elif(re.match("[0-9]{4}\-[0-9]{2}\-[0-9]{2}\s[0-9]{2}\:[0-9]{2}\:[0-9]{2}", value)): v = "date " + "yyyy-MM-dd HH:mm:ss" elif(type(value) == type("string")): v = "string" elif(v == ""): print("Data type "+value+" not supported yet.") return v def writeToFile(self): values = self.data[0] file2 = open("Dexhunter_test_result.arff", 'w+' ) self.relationname+="\n" relationString = '@RELATION ' + self.relationname file2.write(''+relationString+'') for i in range(len(self.names)): str2 = "@ATTRIBUTE " + self.names[i] + " " + self.getType( values[i] ) + "\n" file2.write(''+str2+'') file2.write('''''@DATA\n''') for line in self.data: try: file2.write(",".join(line)+"\n") except UnicodeEncodeError: print("cant write Data to file!!") Arff(relationname, filename)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)m3u8格式轉(zhuǎn)換為mp4視頻格式
- Python將圖片批量從png格式轉(zhuǎn)換至WebP格式
- Python實(shí)現(xiàn)把json格式轉(zhuǎn)換成文本或sql文件
- 在Python的struct模塊中進(jìn)行數(shù)據(jù)格式轉(zhuǎn)換的方法
- python中的代碼編碼格式轉(zhuǎn)換問(wèn)題
- python實(shí)現(xiàn)將pvr格式轉(zhuǎn)換成pvr.ccz的方法
- python輕松實(shí)現(xiàn)代碼編碼格式轉(zhuǎn)換
- python實(shí)現(xiàn)批量圖片格式轉(zhuǎn)換
相關(guān)文章
python3中bytes和string之間的互相轉(zhuǎn)換
這篇文章主要介紹了python3中bytes和string之間的互相轉(zhuǎn)換,文中給出了詳細(xì)的介紹和示例代碼,相信對(duì)大家具有一定的參考價(jià)值,有需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-02-02nlp自然語(yǔ)言處理基于SVD的降維優(yōu)化學(xué)習(xí)
這篇文章主要為大家介紹了nlp自然語(yǔ)言處理基于SVD的降維優(yōu)化學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04pytorch中LN(LayerNorm)及Relu和其變相的輸出操作
這篇文章主要介紹了pytorch中LN(LayerNorm)及Relu和其變相的輸出操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05python標(biāo)準(zhǔn)庫(kù)os庫(kù)的函數(shù)介紹
這篇文章主要介紹了python標(biāo)準(zhǔn)庫(kù)os庫(kù)的函數(shù)介紹,需要的朋友可以參考下2020-02-02使用Python寫一個(gè)創(chuàng)意五子棋游戲源代碼
這篇文章主要給大家介紹了關(guān)于使用Python寫一個(gè)創(chuàng)意五子棋游戲的相關(guān)資料,Python作為一種簡(jiǎn)單易上手的編程語(yǔ)言,能夠輕松實(shí)現(xiàn)五子棋游戲,需要的朋友可以參考下2023-08-08Python基于HOG+SVM/RF/DT等模型實(shí)現(xiàn)目標(biāo)人行檢測(cè)功能
這篇文章主要介紹了Python基于HOG+SVM/RF/DT等模型實(shí)現(xiàn)目標(biāo)檢測(cè)[行人檢測(cè)],今天這里并不是說(shuō)要做出怎樣的效果,而是基于HOG+SVM來(lái)實(shí)踐機(jī)器學(xué)習(xí)檢測(cè)的流程,需要的朋友可以參考下2022-06-06