|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
有一组文本格式气味数据,有九个column,分别是'Time','Temperature','MICS 5521 readings','MICS 5135 readings','TGS 2602 readings','TGS 2600 readings','TGS 2611 readings','TGS 2620 readings',文本中数据的样式如下所示:
0.000000 0.000000 0.000000 0.151367 0.151367 0.799561 0.653076 1.832275 1.968994
0.140000 0.000000 0.000000 0.151367 0.151367 0.800781 0.653076 1.832275 1.968994
0.280000 0.000000 0.000000 0.151367 0.152588 0.799561 0.654297 1.832275 1.968994
0.420000 0.000000 0.000000 0.151367 0.151367 0.799561 0.653076 1.832275 1.971436
0.560000 0.000000 0.000000 0.151367 0.151367 0.799561 0.653076 1.832275 1.968994
0.700000 0.000000 0.000000 0.151367 0.151367 0.800781 0.653076 1.832275 1.972656
0.840000 0.000000 0.000000 0.151367 0.152588 0.799561 0.653076 1.832275 1.973877
0.980000 0.000000 0.000000 0.151367 0.151367 0.799561 0.653076 1.832275 1.972656
1.120000 0.000000 0.000000 0.151367 0.151367 0.800781 0.653076 1.832275 1.977539
1.260000 0.000000 0.000000 0.151367 0.152588 0.800781 0.653076 1.832275 1.971436
1.400000 0.000000 0.000000 0.151367 0.152588 0.799561 0.653076 1.832275 1.972656
1.540000 0.000000 0.000000 0.151367 0.151367 0.799561 0.653076 1.832275 1.977539
1.680000 0.000000 0.000000 0.151367 0.151367 0.800781 0.653076 1.832275 1.973877
分别对应这九个column,想用Python把这些数据做成表格,部分代码如下:
mport pandas as pd
import numpy as np
#创建特征列表.
column_names=['Time','Temperature','MICS 5521 readings','MICS 5135 readings','TGS 2602 readings','TGS 2600 readings','TGS 2611 readings','TGS 2620 readings']
#使用文件系统将文本以可读写的形式存为data
f = open("Acetone_dataset_2011-03-25_10h39m39s.rawlog_gasSensors(1).txt","r+")
data = f
在用文件系统之后,data没有办法print出来,是因为不支持读写,请问现在有什么方法可以把这个文本文件制成表格,之后可能用朴素贝叶斯对其进行分类和预测,有人能把这个制成表格吗?谢谢
- import openpyxl
- def savetoexcel(data,fields,sheetname,wbname):
- print("写入excel:")
- wb=openpyxl.load_workbook(filename=wbname)
- sheet=wb.active
- sheet.title=sheetname
- field=1
- for field in range(1,len(fields)+1): # 写入表头
- _=sheet.cell(row=1,column=field,value=str(fields[field-1]))
- row1=1
- col1=0
- for row1 in range(2,len(data)+2): # 写入数据
- for col1 in range(1,len(data[row1-2])+1):
- _=sheet.cell(row=row1,column=col1,value=str(data[row1-2][col1-1]))
- wb.save(filename=wbname)
- print("保存成功")
- #新建excel
- def creatwb(wbname):
- wb=openpyxl.Workbook()
- wb.save(filename=wbname)
- print ("新建Excel:"+wbname+"成功")
- data = [
- [0.000000 ,0.000000, 0.000000 ,0.151367, 0.151367 ,0.799561, 0.653076 ,1.832275, 1.968994 ],
- [0.140000, 0.000000, 0.000000 ,0.151367 ,0.151367, 0.800781, 0.653076 ,1.832275 ,1.968994 ],
- [0.280000 ,0.000000 ,0.000000 ,0.151367 ,0.152588 ,0.799561 ,0.654297, 1.832275 ,1.968994 ],
- [0.420000 ,0.000000, 0.000000 ,0.151367, 0.151367 ,0.799561, 0.653076, 1.832275, 1.971436 ],
- [0.560000 ,0.000000, 0.000000 ,0.151367, 0.151367 ,0.799561, 0.653076 ,1.832275 ,1.968994 ],
- [0.700000 ,0.000000 ,0.000000, 0.151367, 0.151367 ,0.800781, 0.653076 ,1.832275 ,1.972656 ],
- [0.840000 ,0.000000, 0.000000, 0.151367, 0.152588 ,0.799561 ,0.653076 ,1.832275 ,1.973877 ],
- [0.980000, 0.000000 ,0.000000 ,0.151367, 0.151367 ,0.799561 ,0.653076 ,1.832275, 1.972656 ],
- [1.120000 ,0.000000, 0.000000 ,0.151367 ,0.151367 ,0.800781,0.653076 ,1.832275 ,1.977539 ],
- [1.260000 ,0.000000, 0.000000 ,0.151367,0.152588 ,0.800781 ,0.653076 ,1.832275, 1.971436 ],
- [1.400000 ,0.000000 ,0.000000 ,0.151367 ,0.152588, 0.799561 ,0.653076 ,1.832275 ,1.972656 ],
- [1.540000, 0.000000, 0.000000, 0.151367 ,0.151367, 0.799561, 0.653076, 1.832275 ,1.977539 ],
- [1.680000, 0.000000, 0.000000 ,0.151367, 0.151367 ,0.800781 ,0.653076, 1.832275 ,1.973877 ]
- ]
- fields = ['Time','Temperature','MICS 5521 readings','MICS 5135 readings','TGS 2602 readings','TGS 2600 readings','TGS 2611 readings','TGS 2620 readings','aaa']
- sheetname = 'first'
- wbname = 'myexcel.xlsx'
- creatwb(wbname)
- savetoexcel(data,fields,sheetname,wbname)
复制代码
你的列标题只给了8个?
|
|