zoukankan      html  css  js  c++  java
  • Python文件比较/Pexpect/Django导出数据

    文件比较

    import difflib
    
    t1 = open('d:/text1.txt','r')
    t2 = open('d:/text2.txt','r')
    t1_line = t1.read()
    t2_line = t2.read()
    
    t1_lines = t1_line.splitlines()
    t2_lines = t2_line.splitlines()
    
    d = difflib.HtmlDiff()
    print(d.make_file(t1_lines,t2_lines))
    
    t1.close()
    t2.close()
    

    Pexpect Login:

    #!/usr/bin/env python
    import pexpect
    import time,re
    def get(login_ip,target):
            login = 'telnet %s' % login_ip
            tn = pexpect.spawn(login,timeout = 300)
            tn.expect('Username:')
            username = 'pingtest'
            password = 'pwdtest'
            tn.sendline(username)
            tn.expect('Password:')
            tn.sendline(password)
            tn.expect('#')
            ping = 'ping %s re 1000  df '% target
            tn.sendline(ping)
            time.sleep(25)
            tn.expect('#')
            a = tn.before
            tn.sendline('exit')
            print a
    
    
    if __name__ == '__main__':
    #       login_ip = input('pls input the login ip: ')
    #       target = input('pls input the test ip: ')
            login_ip = raw_input('pls input the login ip: ')
            target = raw_input('pls input the test ip: ')
            get(login_ip,target)
    

    Django导出数据到Excel:

    #此脚本可直接将Django的数据库导出到Eclcl脚本,需要用到xlwt的库,然后可以直接在Html中调用就可以了。
    # <a class='excel' href='{% url 'excel' %}'>导出EXCEL</a>
    
    import xlwt
    import StringIO,os
    
    def excel(request):
        vrf_list = Vrf_model.objects.all().order_by('-vrf_rd')
        if vrf_list:
            # 创建工作薄
            response = HttpResponse(content_type='application/vnd.ms-excel')
            response['Content-Disposition'] = 'attachment;filename=asdfa.xls'
            ws = xlwt.Workbook(encoding='utf-8')
            w = ws.add_sheet(u'sheet1')
            w.write(0,0,'id')
            w.write(0,1,'customer')
            w.write(0,2,'vrf_name')
            w.write(0,3,'vrf_rd')
            w.write(0,4,'yongtu')
            #w.write(0,5,'create_date')
            #w.write(0,6,'change_date')
            # 写入数据  
            excel_row = 1
            for i in vrf_list:
                w.write(excel_row,0,i.id)
                w.write(excel_row,1,i.customer)
                w.write(excel_row,2,i.vrf_name)
                w.write(excel_row,3,i.vrf_rd)
                w.write(excel_row,4,i.yongtu)
                #w.write(excel_row,5,i.create_date)
                #w.write(excel_row,6,i.change_date)
                excel_row += 1
            output = StringIO.StringIO() 
            ws.save(output)
            output.seek(0)
            response.write(output.getvalue())
            return response
    		
    		
    		
    

      

  • 相关阅读:
    Vue知识总结
    Excel使用技巧
    java框架总结
    java反射学习总结
    java虚拟机
    vue学习知识
    mysql索引知识
    前端函数定义及表格总结
    SpringMVC异常处理
    restful风格的概念
  • 原文地址:https://www.cnblogs.com/syother/p/6733125.html
Copyright © 2011-2022 走看看