# pip install xlrdimport xlrd
def read_from_xls(filepath,index_col_list): #filepath:读取文件路径,例如:filepath = r'D:/Python_workspace/Felix_test/motion_test/running_7_29_21time_amsckg_getmRealAcc_S_pre.xlsx' #index_col_list:读取列的索引列表,例如第一、二、三、四列为:[1,2,3,4]
# 设置GBK编码 xlrd.Book.encoding = "gbk" rb = xlrd.open_workbook(filepath) #print(rb) sheet = rb.sheet_by_index(0) #表示Excel的第一个Sheet nrows = sheet.nrows data_tmp_x = [] #例如数据为x,y,z坐标数据 data_tmp_y = [] data_tmp_z = [] for index_col in index_col_list: #依次选择第index_col列 for i in range(nrows): tt=i+1 #读取第tt行,除去第一行的列名 if tt >= nrows: break else: tmp = float(sheet.cell_value(tt,index_col)) #读取第几行第几列的数据内容 if index_col == 2: data_tmp_x.append(tmp) elif index_col == 3: data_tmp_y.append(tmp) elif index_col == 4: data_tmp_z.append(tmp) data_tmp = np.mat([data_tmp_x,data_tmp_y,data_tmp_z]) return data_tmp# 使用pandas读取excel# filepath: xlsx文件路径名import pandas as pddata = pd.read_excel(filepath)province_name = data['province'].values.tolist() # province为列名,结果形成列表province_people = data['count'].values.tolist()
## 欢迎有错误进行指正,也可交流改进
来源:博客园
作者:落日峡谷
链接:https://www.cnblogs.com/qi-yuan-008/p/11672761.html