当前位置:网站首页>About the basic operation of xlrd Library (for beginners)

About the basic operation of xlrd Library (for beginners)

2022-06-21 17:52:00 Elite cadre flaw light

xlrd Is one for reading excel The library of , Extremely fast reading , It is easy to use , Is read excel The first choice for Libraries .

The following goes to the text :

        

 To read excel, First you have to get its path 
import xlrd
path=r'C:\Users\gztsrayz\Desktop\ yogurt .xlsx'
wb = xlrd.open_workbook(path)

In fact, this is just a choice , Not really open excel, That's why I recommend xlrd The place of , Reading some big excel When , Omitting the opening and closing steps can save us a lot of time .

 The next step is to narrow down our scope step by step 
name_list = wb.sheet_names()           # obtain excel All table names for , Of course, you can not use this if you know 

ws=wb.sheet_by_name(' Summary ')
#ws=wb.sheet_by_index(1)                 # Select a table according to its name or location 

nrows=ws.nrows                           # Get the maximum number of lines 
ncols=ws.ncols                           # Get the maximum number of columns 
ps: The maximum number of rows and columns obtained here , The empty rows and columns in the middle are also counted ,
    If you don't want it, you can filter out empty rows and columns when you use it 
   
ws.row_values(5)                         # Get the data of the fourth row 
ws.col_values(4)                         # Get the data of the third column 
ws.cell_value(2,2)                       # obtain C3 Cell data 
ps: Through these three lines of code , We can see that ,xlrd While reading the data , The reading positions are all yours ( Input location +1)
     This is different from most other libraries !!

    Through the above code , We found that :xlrd The data read from rows and columns is output in the form of a list , So since it's a list , That means we can customize his order

list1=[]
for i in range(0,nrows):
     # We can even be in for Add judgment under the loop to filter 
     list1.append([ws.cell_value(i,3),ws.cell_value(i,4),ws.cell_value(i,1),
                  ws.cell_value(i,2),ws.cell_value(i,0)])
print(list1)

  There is probably so much useful about reading , At least it should be about the same for beginners .

原网站

版权声明
本文为[Elite cadre flaw light]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211457166365.html