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]

相关文章

最近Python有点火? 给你7个学习它的理由!

最近Python有点火? 给你7个学习它的理由!

Python 是一门更注重可读性和效率的语言,尤其是相较于 Java,PHP 以及 C++ 这样的语言,它的这两个优势让其在开发者中大受欢迎。 诚然,它有点老了,但仍是80后啊 —— 至...

Python网络编程基于多线程实现多用户全双工聊天功能示例

本文实例讲述了Python网络编程基于多线程实现多用户全双工聊天功能。分享给大家供大家参考,具体如下: 在前面一篇《Python网络编程使用select实现socket全双工异步通信功能...

tensorflow: variable的值与variable.read_value()的值区别详解

tensorflow: variable的值与variable.read_value()的值区别详解

问题 查看 tensorflow api manual 时,看到关于 variable.read_value() 的注解如图: 那么在 tensorflow 中,variable的值...

详解python中init方法和随机数方法

1、__init__方法的使用 2、random方法的使用 在python中,有一些方法是特殊的,是以两个下划线开始,两个下划线结束,定义类,最常用的方法就是__init__()方法,这...

Python3内置模块pprint让打印比print更美观详解

概述 在我们使用内置打印函数print时,打印出的Python数据结构对象总是一行的输出的方式,这样对数据结构较复杂或数据较多的对象的显示并不美观,这时我们可以利用pprint输出美化...