pandas判断excel列名是否正确
一、代码
import pandas as pd import os class ExcelColumn(object): def __init__(self, path, columns=None, converters=None): self.path = path self.columns = columns or [] self.converters = converters self.df = self.read() def read(self): if not os.path.exists(self.path): raise BaseException("excel路径错误或不存在") df = pd.read_excel(self.path, converters=self.converters) return df @property def is_true(self): columns = self.df.columns.tolist() if not isinstance(self.columns,list): raise BaseException("columns格式错误,必须为list") if not self.columns: return False for i in range(len(self.columns)): if columns[i] !=self.columns[i]: return False if columns[i] not in self.columns: return False else: return True path = r"********.xlsx" r=["A", "B", "C", "D", "F"] ef = ExcelColumn(path,r) print(ef.is_true)