Python data analysis of the Excel file (1)

Reference:
"Python Data Analysis", author [US] Clinton W. Brownley, translator Chenguang Xin, China Industry and Information Publishing Group, the People's Posts and Telecommunications Press

Description of the Excel file

  Microsoft Excel is a Microsoft Windows and Apple Macintosh computers use operating system written in a spreadsheet, it is almost everywhere, it is an indispensable business tool. Python can process the data using Excel files.
  Python's csv module with different, Python standard module does not deal with Excel files. We need to install xlrd and xlwt two modules.

Introspection Excel workbook

  We create an Excel workbook, and add three separate work table, as shown below.
Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description  Excel files and CSV files is different in at least two important ways. First, CSV file is a plain text file, Excel file is not a plain text file, we can not open it in a text editor and view the data. Secondly, with different CSV file, an Excel workbook is designed to contain multiple sheets.
  A workbook by introspection, we can begin processing before the actual data in the workbook, data type and amount of data to check the number of worksheets and each worksheet.
  Here we use the workbook above Python analysis to determine the number of worksheets in the workbook number, name, and each worksheet ranks.

#!/usr/bin/env python3

import sys
from xlrd import open_workbook

input_file = sys.argv[1]

workbook = open_workbook(input_file)
print('Number of worksheets: ', workbook.nsheets)
for worksheet in workbook.sheets():
    print("Worksheet name: ", worksheet.name, "\tRows: ", worksheet.nrows, "\tColumns: ", worksheet.ncols)

  We explain the above code.

from xlrd import open_workbook

  This line of code into xlrd module open_workbook()functions to read and analyze Excel files.

workbook = open_workbook(input_file)

  This line of code using the open_workbook()function to open an Excel file input, and assigned to an object workbook. This object can be obtained using a single sheet from the workbook.

for worksheet in workbook.sheets():
    print("Worksheet name: ", worksheet.name, "\tRows: ", worksheet.nrows, "\tColumns: ", worksheet.ncols)

  forLoop iterations between all the worksheets in the workbook. workbookAn object sheets()method workbook may identify all worksheets. printStatement uses the workbookobject name, nrows, ncolsproperty to determine the name and number of rows and columns per sheet.
  We run this script on the command line window, get the following output.
Here Insert Picture Description

Published 25 original articles · won praise 9 · views 2161

Guess you like

Origin blog.csdn.net/qq_45554010/article/details/104102682