python实现txt文件格式转换为arff格式
作者:君的名字 时间:2022-05-11 16:06:06
本文实例为大家分享了python实现txt文件格式转换为arff格式的具体代码,供大家参考,具体内容如下
将文件读取出来的时候默认都是字符型的,所以有转换出来有点问题,但是还是可以用的。
文件要求第一行是你对应的属性名,之后是数字。
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)
来源:https://blog.csdn.net/chichoxian/article/details/41937581
标签:python,txt,arff
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
由prototype_1.3.1进入javascript殿堂-类的初探
2008-05-22 12:44:00
django用户登录和注销的实现方法
2021-01-17 13:55:40
![](https://img.aspxhome.com/file/2023/0/88890_0s.png)
ASP实现GB2312字符与区位码的相互转换
2009-12-28 10:27:00
ACCESS数据库遇日文时内存溢出的最简单处理办法
2009-07-09 14:04:00
pytorch 实现二分类交叉熵逆样本频率权重
2021-04-29 00:25:29
![](https://img.aspxhome.com/file/2023/5/90365_0s.png)
pyqt5对用qt designer设计的窗体实现弹出子窗口的示例
2023-01-28 12:58:07
python 对txt中每行内容进行批量替换的方法
2022-12-29 21:37:45
![](https://img.aspxhome.com/file/2023/1/67841_0s.jpg)
分享整理的12条sql语句连同数据
2012-07-11 16:14:59
Python的另外几种语言实现
2023-05-29 15:39:03
Python Django框架设计模式详解
2021-01-16 12:52:40
![](https://img.aspxhome.com/file/2023/4/72864_0s.jpg)
Oracle如何直接运行OS命令(上)第1/2页
2010-07-30 12:54:00
技术性击倒与抬杠
2009-02-12 12:28:00
python 获取字典特定值对应的键的实现
2022-07-01 19:25:21
pycharm设置python文件模板信息过程图解
2023-03-21 14:46:22
![](https://img.aspxhome.com/file/2023/0/81620_0s.png)
WEB前端开发经验总结
2009-07-20 18:01:00
![](https://img.aspxhome.com/file/UploadPic/20097/20/standard-1-62s.jpg)
Python 分发包中添加额外文件的方法
2023-02-15 08:43:11
SQL Server保障数据一致性的法宝
2008-12-26 15:21:00
xml文件调用css
2008-09-05 17:12:00
Pygame实战练习之飞机大战游戏
2021-01-13 13:11:25
![](https://img.aspxhome.com/file/2023/3/72723_0s.gif)
ASP开发中有用的函数(function)集合(3)
2008-10-14 17:20:00