# -*- coding:utf-8 -*- # author:021王掌柜 import xlrd import pandas as pd # xlrd 读取数据方法 def read_data(path): # 打开Excel文件,获取sheet对象 work_book = xlrd.open_workbook(path) # 获取第一个表单sheet sheet = work_book.sheet_by_index(0) # 一次性按行获取所有数据(每行都是一个列表) all_data = sheet._cell_values return all_data # data_list = read_data(r"C:\Users\王掌柜\Desktop\测试.xlsx") # for info in data_list: # print(info) """ pandas 读取 Excel 文件(.xlsx)时报错如下: raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported') xlrd.biffh.XLRDError: Excel xlsx file; not supported 二、报错原因 xlrd 版本过低,只支持读取 .xls 文件 三、解决方案 1、方法一 先卸载低版本的 xlrd,然后安装新版本: pip uninstall xlrd pip install xlrd==1.2.0 2、方法二 用 openpyxl 代替 xlrd 打开 .xlsx 文件: df = pandas.read_excel(‘data.xlsx’,engine='openpyxl') """ # pandas 读取 Excel frame = pd.read_excel(r"C:\Users\王掌柜\Desktop\测试.xlsx") data = pd.DataFrame(frame) # 单列去重 data.drop_duplicates(subset=['关键词'], keep='last', inplace=True) # drop_duplicates用法: # subset=‘['需要去重复的列名']’,keep=‘遇到重复时保留第一个False还是保留最后一个'last’, # inplace=‘去除重复项,还是保留重复项的副本’ data.to_excel(r'C:\Users\王掌柜\Desktop\out.xlsx', index=False) print('合并完成')
Excel数据的读取与去重
最新推荐文章于 2024-07-12 14:33:35 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)