分享

学习记录:python serial 库&excel操作

 乘舟泛海赏雨 2017-04-28

1,背景

前段时间参与了一个项目,需要对路由设备进行自动化扫描,路由设备获取到shell的方式很多,但是既然要通用,就必须要使用串口方式,就学习了一下,做个记录。

由于没有找到相应通用的方法(lz,rz需要编译无法自动化),在串口间传输文件,因此看看来,最后还是要使用ftp来传输,麻烦!仅对串口做个记录吧。

2,参考文章

比较详细的记录:http://icodding./2016/05/python-pyserial.html

需要调试的demo:http://www.cnblogs.com/Dreamxi/p/4109450.html

当然官方文档:http://pyserial./en/latest/pyserial.html

github:https://github.com/pyserial/pyserial

3,一个执行命令不断读取的小模块

远程执行ls命令并获得回显结果:

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. #coding=utf-8  
  2.   
  3. import serial  
  4. def romote_ls_cmd1():  
  5.     ser = serial.Serial("com1", 115200, timeout=2)  
  6.     print(ser.is_open)  
  7.     if ser.is_open:  
  8.         ser.write("ls -l\r\n".encode())  
  9.       
  10.         while True:  
  11.             data = ser.readline().decode("utf-8")  
  12.             #此处也可以使用readlines一次读出来。  
  13.             print(data.strip())  
  14.             if len(data) == 0:  
  15.                 break  
  16.     else:  
  17.         print("ser is not open")  
  18.     ser.close()   
  19.         
  20. #也可以把read(size) + inWaiting()          
  21. def romote_ls_cmd2():  
  22.     ser = serial.Serial("com1", 115200, timeout=2)  
  23.     print(ser.is_open)  
  24.     if ser.is_open:  
  25.         while True:  
  26.             ser.write("ls -l\r\n".encode())  
  27.             size = ser.inWaiting()  
  28.               
  29.             if size:  
  30.                 data = ser.read(size).decode("utf-8")  
  31.                 print(data.strip())  
  32.             if len(data) == 0:  
  33.                 break  
  34.     else:  
  35.         print("ser is not open")  
  36.     ser.close()        
  37.       

4,自动化运行的结果要求使用excel保存

因此又接触了几个excel的模块

针对sheet的两个操作,需要格外记忆一下

4.1 rename sheet

http:///questions/13785306/change-name-of-an-excel-worksheet-after-reading-the-file-in-python

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. from xlutils.copy import copy  
  2. from xlrd import open_workbook  
  3.   
  4. # open the file you're interested  
  5. rb = open_workbook('some_document.xlsx')  
  6.   
  7. # copy it to a writable variant  
  8. wb = copy(rb)  
  9.   
  10. # find the index of a sheet you wanna rename,  
  11. # let's say you wanna rename Sheet1  
  12. idx = rb.sheet_names().index('Sheet1')  
  13.   
  14. # now rename the sheet in the writable copy  
  15. wb.get_sheet(idx).name = u'Renamed Sheet1'  
  16.   
  17. # save the new spreadsheet  
  18. wb.save('new_some_document.xlsx')  
  19.   
  20. # done  

4.2 append sheet

http:///questions/38081658/adding-a-sheet-to-an-existing-excel-worksheet-without-deleting-other-sheet

[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. import xlrd, xlwt  
  2. from xlutils.copy import copy as xl_copy  
  3.   
  4. # open existing workbook  
  5. rb = xlrd.open_workbook('ex.xls', formatting_info=True)  
  6. # make a copy of it  
  7. wb = xl_copy(rb)  
  8. # add sheet to workbook with existing sheets  
  9. Sheet1 = wb.add_sheet('Sheet1')  
  10. wb.save('ex.xls')  

4.3 读写的笔记

参考:http://www.cnblogs.com/weiok/p/5369741.html


[python] view plain copy
在CODE上查看代码片派生到我的代码片
  1. <pre code_snippet_id="2021488" snippet_file_name="blog_20161202_1_315349"></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <link rel="stylesheet" href="http://static.blog.csdn.net/public/res-min/markdown_views.css?v=1.0">  
  6.                         

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多