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创建临时文件夹的方法

本文实例讲述了python创建临时文件夹的方法。分享给大家供大家参考。具体实现方法如下: import tempfile, os tempfd, tempname = tempfi...

使用pyinstaller打包PyQt4程序遇到的问题及解决方法

使用pyinstaller打包PyQt4程序遇到的问题及解决方法

python pyinstaller pyqt4 打包 QWindows 最近在做课设,用pyqt设计界面。然后用pyinstaller打包程序后,双击运行却总是闪退,后来将exe文件拖...

Python字符串大小写转换拼接删除空白

1.字符串大小写转换 string.title() #将字符串中所有单词的首字母以大写形式显示 string.upper() #将字符串中所有字母转化为大写字母 stri...

pycharm 使用心得(二)设置字体大小

pycharm 使用心得(二)设置字体大小

 步骤: Settings-->Editor-->Colors & Fonts-->Font 然后在size那里调整。...

python重试装饰器的简单实现方法

简单实现了一个在函数执行出现异常时自动重试的装饰器,支持控制最多重试次数,每次重试间隔,每次重试间隔时间递增。 最新的代码可以访问从github上获取 https://github.co...