| 
                         json转str 
- import json 
 - str = '{"name": "zyl", "age": "two"}' 
 - p = json.loads(str) 
 - print(p) 
 - print(type(p)) 
 
  
json转str 
使用json.dumps的方法,可以将json对象转化为字符串。 
- s = {'name':'zyl','age':'22'} 
 - s = json.dumps(s) 
 
  
5. python读取excel 
步骤 
    - 安装python官方Excel库–>xlrd
 
    - 获取Excel文件位置并读取
 
    - 读取sheet
 
    - 读取指定rows和cols内容
 
 
示例 
- # -*- coding: utf-8 -*- 
 - import xlrd 
 - from datetime import date,datetime 
 - def read_excel(): 
 -  
 - #文件位置 
 -  
 - ExcelFile=xlrd.open_workbook(r'C:UsersAdministratorDesktopTestData.xlsx') 
 -  
 - #获取目标EXCEL文件sheet名 
 -  
 - print ExcelFile.sheet_names() 
 -  
 - #若有多个sheet,则需要指定读取目标sheet例如读取sheet2 
 -  
 - #sheet2_name=ExcelFile.sheet_names()[1] 
 -  
 - #获取sheet内容【1.根据sheet索引2.根据sheet名称】 
 -  
 - #sheet=ExcelFile.sheet_by_index(1) 
 -  
 - sheet=ExcelFile.sheet_by_name('TestCase002') 
 -  
 - #打印sheet的名称,行数,列数 
 -  
 - print sheet.name,sheet.nrows,sheet.ncols 
 -  
 - #获取整行或者整列的值 
 -  
 - rows=sheet.row_values(2)#第三行内容 
 -  
 - cols=sheet.col_values(1)#第二列内容 
 -  
 - print cols,rows 
 -  
 - #获取单元格内容 
 -  
 - print sheet.cell(1,0).value.encode('utf-8') 
 -  
 - print sheet.cell_value(1,0).encode('utf-8') 
 -  
 - print sheet.row(1)[0].value.encode('utf-8') 
 -  
 - #打印单元格内容格式 
 -  
 - print sheet.cell(1,0).ctype 
 -  
 - if__name__ =='__main__': 
 -  
 - read_excel() 
 
  
6. python 截图 
python实现截图功能,windows环境下,需要用到PIL库。 
安装: 
- pip install Pillow 
 
  
示例: 
- from PIL import ImageGrab 
 - bbox = (x1, y1, x2,y2 ) 
 - # x1: 开始截图的x坐标;x2:开始截图的y坐标;x3:结束截图的x坐标;x4:结束截图的y坐标 
 - im = ImageGrab.grab(bbox) 
 - im.save('as.png')#保存截图文件的路径 
 
  
7. ipython 
最后介绍的示一个强大的python工具——IPython 。 
IPython 支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多实用功能和函数; 
它是一个 for Humans 的 Python 交互式 shell,用了它之后你就不想再用自带的 Python shell 了。 
                         (编辑:我爱故事小小网_铜陵站长网) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |