python中查找excel某一列的重复数据 剔除之后打印

yipeiwu_com6年前Python基础
1.在python中excel的简单读写操作,推荐使用xlrd(特别是读操作)

2.到http://pypi.python.org/pypi/xlrd 去下载 xlrd库;

3.工程代码如下:

复制代码 代码如下:

    import xlrd  

    def open_excel(fileName="simple.xls"): 
        try: 
            fileHandler = xlrd.open_workbook(fileName) 
            return fileHandler 
        except Exception, e: 
            print str(e)     

    def scan_excel(sheet_name1=u'Sheet1'): 
        handler = open_excel() 
        page = handler.sheet_by_name(sheet_name1) 
        return page 

    def trim_cols(index=0): 
        page = scan_excel() 
        col1 = page.col_values(index) 
        col2 = [] 

        for item in col1: 
            if item not in col2: 
                col2.append(item) 
        print col1 
        print col2 

    def main(): 
        trim_cols() 

    if __name__ == "__main__": 
        main() 


打印结果:
[1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4.0]
[1.0, 2.0, 3.0, 4.0]

相关文章

将pip源更换到国内镜像的详细步骤

将pip源更换到国内镜像的详细步骤

pip是一个很好用的第三方库安装方式,但是默认的源没法连接,就算有时候可以成功率也很低,所以换成国内镜像源比较方便。 将pip源更换到国内镜像 用pip管理工具安装库文件时,默认使用国...

Python Selenium Cookie 绕过验证码实现登录示例代码

Python Selenium Cookie 绕过验证码实现登录示例代码

之前介绍过通过cookie 绕过验证码实现登录的方法。这里并不多余,会增加分析和另外一种方法实现登录。 1、思路介绍  1.1、直接看代码,内有详细注释说明 # File...

Python数据结构与算法之二叉树结构定义与遍历方法详解

本文实例讲述了Python数据结构与算法之二叉树结构定义与遍历方法。分享给大家供大家参考,具体如下: 先序遍历,中序遍历,后序遍历 ,区别在于三条核心语句的位置 层序遍历  采...

python Crypto模块的安装与使用方法

python Crypto模块的安装与使用方法

前言 最开始想尝试在windows下面安装python3.6,虽然python安装成功,但在安装Cryto模块用pip3 install pycrypto老是会报错。老夫搞了半天,最终决...

Python三级菜单的实例

要求: 打印省、市、县三级菜单 可返回上一级 可随时退出程序 版本1 # _author : Ahern Li # @_date : 2017/9/12 menu = { '浙...