|
环境:win7系统、64bit、CPU、Anaconda3-4.2.0-Windows-x86_64
软件:Anaconda3-4.2.0-Windows-x86_64、VS2013
读取和写入Excel:
xlrd和xlwt,习惯用array来操作
- # -*- coding: utf-8 -*-
- """
- Created on Wed Jul 19 19:43:01 2017
- @author: Administrator
- """
- import xlrd
- import numpy
- from matplotlib import pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D
- excel = xlrd.open_workbook('ysw.xlsx');
- # 获取第一个sheet
- sheet1 = excel.sheets()[0]
- # 获取数据行数
- nrows = sheet1.nrows;
- # 获取数据列数
- ncols = sheet1.ncols;
- # 获取第3列的数据,数据的列数从0开始计数
- value = sheet1.col_values(2)
- # 获取第3行的数据,数据的行列数从0开始计数
- value1 = sheet1.row_values(2)
- # 获取制定行列的数据
- row = 2;
- col = 2;
- value2 = sheet1.cell(row,col).value;
- # 获取整个excel表格数据
- data = [[0 for x in range(ncols)]]*(nrows-1)
- data = numpy.array(data,dtype='float64');
- for i in range(nrows-1):
- data[i][0] = sheet1.cell(i+1,0).value;
- data[i][1] = sheet1.cell(i+1,1).value;
- data[i][2] = sheet1.cell(i+1,2).value;
- data[i][3] = sheet1.cell(i+1,3).value;
- data[i][4] = sheet1.cell(i+1,4).value;
- # 转置
- x,y,z = data.T[0], data.T[1],data.T[2]
- fig = plt.figure();
- # 创建一个三维的视图
- ax = plt.subplot(111,projection='3d');
- # 散点图
- ax.scatter(x,y,z,c='b');
- ax.set_xlabel("x")
- ax.set_ylabel("y")
- ax.set_zlabel("z")
- ax.set_title("scatter diagram")
- plt.show()
- # 将data写入Excel
- # 创建一个book
- import xlwt
- book = xlwt.Workbook(encoding='utf-8', style_compression=0);
- # 在book里面添加一个sheet
- sheet = book.add_sheet('halcom.cn',cell_overwrite_ok = True);
- # 写入数据
- sheet.write(0,0,'www.halcom.cn');
- for i in range(12):
- sheet.write(i+1,0,data[i][0]);
- sheet.write(i+1,1,data[i][1]);
- sheet.write(i+1,2,data[i][2]);
- sheet.write(i+1,3,data[i][3]);
- sheet.write(i+1,4,data[i][4]);
-
- book.save('D:\Deep_learning_TensorFlow_python\Learning_DayByDay\Excel\data.xls')
复制代码
book.save需要写为.xls后缀的Excel。
|
|