鱼C论坛

 找回密码
 立即注册
查看: 1506|回复: 4

[已解决]用python处理文件遇到的困难

[复制链接]
发表于 2017-10-21 11:14:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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出来,是因为不支持读写,请问现在有什么方法可以把这个文本文件制成表格,之后可能用朴素贝叶斯对其进行分类和预测,有人能把这个制成表格吗?谢谢
最佳答案
2017-10-21 11:33:02
  1. import openpyxl


  2. def savetoexcel(data,fields,sheetname,wbname):
  3.     print("写入excel:")
  4.     wb=openpyxl.load_workbook(filename=wbname)
  5.     sheet=wb.active
  6.     sheet.title=sheetname
  7.     field=1
  8.     for field in range(1,len(fields)+1):   # 写入表头
  9.         _=sheet.cell(row=1,column=field,value=str(fields[field-1]))
  10.     row1=1
  11.     col1=0
  12.     for row1 in range(2,len(data)+2):  # 写入数据
  13.         for col1 in range(1,len(data[row1-2])+1):
  14.             _=sheet.cell(row=row1,column=col1,value=str(data[row1-2][col1-1]))
  15.     wb.save(filename=wbname)
  16.     print("保存成功")

  17. #新建excel
  18. def creatwb(wbname):
  19.     wb=openpyxl.Workbook()
  20.     wb.save(filename=wbname)
  21.     print ("新建Excel:"+wbname+"成功")


  22. data = [
  23. [0.000000 ,0.000000, 0.000000 ,0.151367, 0.151367 ,0.799561, 0.653076 ,1.832275, 1.968994 ],
  24. [0.140000, 0.000000, 0.000000 ,0.151367 ,0.151367, 0.800781, 0.653076 ,1.832275 ,1.968994 ],
  25. [0.280000 ,0.000000 ,0.000000 ,0.151367 ,0.152588 ,0.799561 ,0.654297, 1.832275 ,1.968994  ],
  26. [0.420000 ,0.000000, 0.000000 ,0.151367, 0.151367 ,0.799561, 0.653076, 1.832275, 1.971436 ],
  27. [0.560000 ,0.000000, 0.000000 ,0.151367, 0.151367 ,0.799561, 0.653076 ,1.832275 ,1.968994  ],
  28. [0.700000 ,0.000000 ,0.000000, 0.151367, 0.151367 ,0.800781, 0.653076 ,1.832275 ,1.972656  ],
  29. [0.840000 ,0.000000, 0.000000, 0.151367, 0.152588 ,0.799561 ,0.653076 ,1.832275 ,1.973877  ],
  30. [0.980000, 0.000000 ,0.000000 ,0.151367, 0.151367 ,0.799561 ,0.653076 ,1.832275, 1.972656  ],
  31. [1.120000 ,0.000000, 0.000000 ,0.151367 ,0.151367 ,0.800781,0.653076 ,1.832275 ,1.977539 ],
  32. [1.260000 ,0.000000, 0.000000 ,0.151367,0.152588 ,0.800781 ,0.653076 ,1.832275, 1.971436  ],
  33. [1.400000 ,0.000000 ,0.000000 ,0.151367 ,0.152588, 0.799561 ,0.653076 ,1.832275 ,1.972656  ],
  34. [1.540000, 0.000000, 0.000000, 0.151367 ,0.151367, 0.799561, 0.653076, 1.832275 ,1.977539 ],
  35. [1.680000, 0.000000, 0.000000 ,0.151367, 0.151367 ,0.800781 ,0.653076, 1.832275 ,1.973877 ]
  36. ]

  37. fields = ['Time','Temperature','MICS 5521 readings','MICS 5135 readings','TGS 2602 readings','TGS 2600 readings','TGS 2611 readings','TGS 2620 readings','aaa']
  38. sheetname = 'first'
  39. wbname = 'myexcel.xlsx'
  40. creatwb(wbname)
  41. savetoexcel(data,fields,sheetname,wbname)
复制代码


你的列标题只给了8个?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-10-21 11:24:13 | 显示全部楼层
f = open()
之后要 data = f.read()

然后就是用str的split进行分割处理了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-21 11:33:02 | 显示全部楼层    本楼为最佳答案   
  1. import openpyxl


  2. def savetoexcel(data,fields,sheetname,wbname):
  3.     print("写入excel:")
  4.     wb=openpyxl.load_workbook(filename=wbname)
  5.     sheet=wb.active
  6.     sheet.title=sheetname
  7.     field=1
  8.     for field in range(1,len(fields)+1):   # 写入表头
  9.         _=sheet.cell(row=1,column=field,value=str(fields[field-1]))
  10.     row1=1
  11.     col1=0
  12.     for row1 in range(2,len(data)+2):  # 写入数据
  13.         for col1 in range(1,len(data[row1-2])+1):
  14.             _=sheet.cell(row=row1,column=col1,value=str(data[row1-2][col1-1]))
  15.     wb.save(filename=wbname)
  16.     print("保存成功")

  17. #新建excel
  18. def creatwb(wbname):
  19.     wb=openpyxl.Workbook()
  20.     wb.save(filename=wbname)
  21.     print ("新建Excel:"+wbname+"成功")


  22. data = [
  23. [0.000000 ,0.000000, 0.000000 ,0.151367, 0.151367 ,0.799561, 0.653076 ,1.832275, 1.968994 ],
  24. [0.140000, 0.000000, 0.000000 ,0.151367 ,0.151367, 0.800781, 0.653076 ,1.832275 ,1.968994 ],
  25. [0.280000 ,0.000000 ,0.000000 ,0.151367 ,0.152588 ,0.799561 ,0.654297, 1.832275 ,1.968994  ],
  26. [0.420000 ,0.000000, 0.000000 ,0.151367, 0.151367 ,0.799561, 0.653076, 1.832275, 1.971436 ],
  27. [0.560000 ,0.000000, 0.000000 ,0.151367, 0.151367 ,0.799561, 0.653076 ,1.832275 ,1.968994  ],
  28. [0.700000 ,0.000000 ,0.000000, 0.151367, 0.151367 ,0.800781, 0.653076 ,1.832275 ,1.972656  ],
  29. [0.840000 ,0.000000, 0.000000, 0.151367, 0.152588 ,0.799561 ,0.653076 ,1.832275 ,1.973877  ],
  30. [0.980000, 0.000000 ,0.000000 ,0.151367, 0.151367 ,0.799561 ,0.653076 ,1.832275, 1.972656  ],
  31. [1.120000 ,0.000000, 0.000000 ,0.151367 ,0.151367 ,0.800781,0.653076 ,1.832275 ,1.977539 ],
  32. [1.260000 ,0.000000, 0.000000 ,0.151367,0.152588 ,0.800781 ,0.653076 ,1.832275, 1.971436  ],
  33. [1.400000 ,0.000000 ,0.000000 ,0.151367 ,0.152588, 0.799561 ,0.653076 ,1.832275 ,1.972656  ],
  34. [1.540000, 0.000000, 0.000000, 0.151367 ,0.151367, 0.799561, 0.653076, 1.832275 ,1.977539 ],
  35. [1.680000, 0.000000, 0.000000 ,0.151367, 0.151367 ,0.800781 ,0.653076, 1.832275 ,1.973877 ]
  36. ]

  37. fields = ['Time','Temperature','MICS 5521 readings','MICS 5135 readings','TGS 2602 readings','TGS 2600 readings','TGS 2611 readings','TGS 2620 readings','aaa']
  38. sheetname = 'first'
  39. wbname = 'myexcel.xlsx'
  40. creatwb(wbname)
  41. savetoexcel(data,fields,sheetname,wbname)
复制代码


你的列标题只给了8个?

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +1 收起 理由
驻火蚁 + 5 + 5 + 1

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-21 19:46:52 | 显示全部楼层
wbname的作用主要是社么?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-21 19:51:26 | 显示全部楼层
驻火蚁 发表于 2017-10-21 19:46
wbname的作用主要是社么?

wbname = 'myexcel.xlsx',就是一个excel的文件名
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-29 00:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表