Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read excel with python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use python to read excel", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use python to read excel" bar!

If you need to extract part of the data from an excel, just open the excel and paste and copy it. If you need to extract part of the data from 100 excel in the same way, then you should write a python script, the time to write the script must be less than the time you spend manually operating 100 excel, this is the value and meaning of the program. To read excel, you can use xlrd as a library.

Step one, open the file

The second step is to get sheet

Step 3, read the data

From xlrd import open_workbook

Book = open_workbook ('stu.xlsx') # Open the file sheet = book.sheet_by_name ("student information") # to get sheet

# read data programming language, never automatically solve a practical application problem, it only provides the most basic operations, such as addition, subtraction, multiplication and division. On the basis of these basic operations, people encapsulate some libraries, but these libraries are only targeted to provide some basic operations at the business level. Everyone can have their own ideas on how to use them in specific applications.

There are only three ways to read data in excel

Read the data of a row

Read the data of a column

Read the data of a cell

Is it a little disappointed that what python can do seems to be very simple and not as magical as it is supposed to be? This is the biggest difference in the understanding of programming between professional programmers and amateur programmers. Laymen think that programming has the power of magic, and professional programmers know very well that the so-called magic is an illusion made up by lines of code.

Now, I'll show you how to find the tallest student from this excel method 1, iterate through it line by line, and compare the height starting with the second row to read the data by row. Compare height nrows = sheet.nrowsmax_height = 0stu_name = "" for i in range (1, nrows): row_data = sheet.row_values (I) if int (row_data [2]) > max_height: max_height = int (row_data [2]) stu_name = row_data [0]

Print (stu_name) # Xiaogang method 2, read the height column and get the index of the maximum height first find the index position of the maximum height in the height column, that is, the row in which the height is located, then the value of the same index position of the surname column is the student's name height_column = sheet.col_values (2) [1:] max_height = max (height_column) index = height_column.index (max_height) # the index of the maximum height

Stu_name = sheet.cell_value (index+1, 0) # read the specified cell print (stu_name) # Xiaogang two ways to read the names of students with maximum height, I used three methods: read by row, read by column, and read data by cell. Xlrd this library can only help us, the rest, you have to rely on your own to complete. If you get here, you don't know how to go on, then it means you still don't have an entry-level programming language, and what you've learned before is just painting the surface of a cat and tiger, and you can't think like a programmer.

Thank you for your reading, the above is the content of "how to use python to read excel", after the study of this article, I believe you have a deeper understanding of how to use python to read excel, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report