标签:usr main sheet instance str tin split 返回 bre
最近发现excel数据量极大,并且通过简单的数据操作不能提取到我需要的数据,如果单独操作,数据量太大耗时太长。#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020-02-24 下午 03:43
# @Author : Zhanxing
# @Site :
# @File : 提取字段.py
# @Software: PyCharm
import openpyxl
import pandas as pd
from xlutils.copy import copy
df=pd.read_excel(‘222.xlsx‘,sheet_name=‘XXX‘)
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):
"""
返回excel每一行的生成器对象
:return:
"""
yield from self.sheet.iter_rows(min_row=2)
def run(excel_file, new_excel):
"""
:param excel_file: 要处理的excel的路径
:param new_excel: 处理后要保存的文件名
: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‘)
标签:usr main sheet instance str tin split 返回 bre
原文地址:https://blog.51cto.com/zhanx/2475595