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 does Python extract data from Excel?

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

Share

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

Recently, I found that the amount of excel data is very large, and the data I need can not be extracted through simple data operation. If I operate alone, the amount of data is too large and time-consuming.

Thinking about a simple way to extract data quickly, I think of Python.

Python manipulates openyxl and pandas used by Excel to operate on Excel.

The code is as follows:

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 2020-02-24 03utf-8 @ Author: Zhanxing# @ Site: # @ File: extract field .py # @ Software: PyCharmimport openpyxlimport pandas as pdfrom xlutils.copy import copydf=pd.read_excel ('222.xlsxexamples) data= (df [' XXX','XXX']]) data.to_excel ("new.xlsx" Index=False) class Excel: def _ _ init__ (self Excel_file): self.excel = openpyxl.load_workbook (excel_file) self.sheet_name = self.excel.get_sheet_names () self.sheet = self.excel.get_sheet_by_name (self.sheet_name [0]) def excel_read (self): "returns the generator object for each row of excel: return:" Yield from self.sheet.iter_rows (min_row=2) def run (excel_file New_excel): "": param excel_file: path to the excel to be processed: param new_excel: file name to be saved after processing: return: "" excel = Excel (excel_file) excel_line = [line for line in excel.excel_read ()] for line in excel_line: for cell in line: if isinstance (cell.value) Int): continue people_name = cell.value.strip ('[]'). Split (',') for num in range (len (people_name)): if 'XX' in people_name [num]: excel.sheet.cell (row=cell.row, column=cell.column Value=people_ name [num]) break excel.excel.save (new_excel) if _ _ name__ = ='_ main__': run ('new.xlsx','11.xlsx')

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

Servers

Wechat

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

12
Report