Pandas Yes, it is python language-written , Super easy to use data processing tools , It also provides a particularly convenient excel Read write function , You can read in one sentence excel All data in the file ：
import pandas as pd dataframe = pd.read_excel(io=file_path_name, header=1)
read_excel Method has many parameters , among header Indicates which line is excel The title line of the file ——Pandas Will use the value in the header row , treat as dataframe Column name in . The number of lines starts from 0 Start counting , for instance excel It's like this ：
Then the above code will put the first 2 The line should be the title line , Read it out dataframe There are two columns , Namely " full name " and " Gender ", It can be used dataframe[' full name '], To get the data in the first column .
But sometimes , We don't know which line is the title line , and excel The file is quite large , For example, there are tens of thousands of lines ,read_excel Time spent at one time , Maybe up to tens of seconds . At this time, we have to read the data at one time , First determine the position of the title line .
Then look for the characteristics of the title line first , For example, we know that the first column is always “ full name ”, We can cycle through all the cells in the first column of the table , Until I find “ full name ” until , Then confirm the title line .
Just right Pandas The bottom layer uses xlrd To read excel, This library does not need to be installed separately , Direct reference , Just add the following lines of code ：
import pandas as pd import xlrd workbook = xlrd.open_workbook(file_path_name) # Open the specified excel file sheet = workbook.sheets() # Read the specified sheet form i = 1 # Suppose by default , The first 2 The row is the header row for i in range(10): value = sheet.cell(i, 0).value # Line number 、 Column number , from 0 Start if value == ' full name ': break workbook.release_resources() dataframe = pd.read_excel(io=file_path_name, header=i)
So no matter which line is the title line , Can be correctly parsed