发布于 2014-09-23 12:52:26 | 368 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Python教程,程序狗速度看过来!

Python编程语言

Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。


本文是一个python结合xlrd库实现的可以查找excel文件中某一列的重复数据的代码,感兴趣的同学参考下.


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]

 



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务