Python은 xlrd, pandas 패키지를 사용하여 Excel에서 데이터를 읽습니다.

1876 단어
#coding=utf-8
# pip install xlrd

import 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 pd
data = pd.read_excel(filepath)
province_name = data['province'].values.tolist() # province ,
province_people = data['count'].values.tolist()
##  , 

좋은 웹페이지 즐겨찾기