python读取ini配置文件
-
config.ini
[browser]
Browsername = Chrome
Url = http://www.baidu.com
[browser1]
Browsername = IE
Url = http://www.baidu.com
configparser读取ini文件内容 #!/usr/bin/python3 # -*- coding: utf-8 -*- # @name : 彭升军 # @TIME : 10:07 上午 # configparser读取ini文件内容 import configparser import os # 定义函数读取配置文件config.ini中的值 def getBrowser(name): ch = configparser.ConfigParser() # 读取路径配置文件 # chpath = os.path.dirname(os.path.abspath('.')) + "/config/config.ini" #这个方法只能找同级的,返回一个目录的绝对路径 chpath = os.path.dirname(os.path.realpath('__file__')) + "/config/config.ini" # 返回指定文件的标准路径,而非软链接所在的路径 print(chpath+" chpathchpath") # 读取文件内容 ch.read(chpath) # 取对应的值 browsername = ch.get('browser', name) print(browsername) return browsername # getBrowser('browsername')
读取.xlsx文件内容方法
首先需要安装 「xlrd」模块,这个库一般是python自带的,如果出现找不到这个模块的情况下使用「pip install xlrd」进行安装即可。
使用方法:
#!/usr/bin/python3 # -*- coding: utf-8 -*- # @name : 彭升军 # @TIME : 4:48 下午 # todo 对excel的操作 import xlrd # todo 打开excle workBook = xlrd.open_workbook(r'/Users/shiheng/pythonProjectUI/data/datashell.xlsx') #print(xl.read()) # 1.获取sheet的名字 # 1.1 获取所有sheet的名字(list类型) allSheetNames = workBook.sheet_names(); print(allSheetNames); for i in range(len(allSheetNames)): print(allSheetNames[i]) print("打印出数据列: {0}".format(workBook.sheet_names())) # 1.2 按索引号获取sheet的名字(string类型) # sheet1Name = workBook.sheet_names()[0]; # print(sheet1Name); print('================') print("有多少数据 {0} 个 shell ".format(workBook.nsheets)) print('================') sh = workBook.sheet_by_index(0) print("表格名: {0} 条数: {1} 记录数: {2}".format(sh.name, sh.nrows, sh.ncols)) print('================') list = [] for rx in range(sh.nrows): print(sh.row(rx)) list.append(sh.row(rx)) # print(sh.nrows(rx)) print('================') print(list) print(list[0]) print(list[0][0]) print(list[0][0].ctype) print(list[0][0].value) print(list[1][0].value) print(list[0][1].value) print(list[1][1].value) print(list[0][2].value) print(list[1][2].value) print(list[0][3].value) print(list[1][3].value) print('================') for x in range(len(list)):# 取出集合中的数据 print('listappent:{}'.format(list[x])) # for j in range(x): # print('listappent:{}'.format(list[x][j].value)) sheet1 = workBook.sheet_by_index(0)#通过索引获取表格 sheet2 = workBook.sheet_by_name('源数据一')#通过名字获取表格 # print(sheet1,sheet2) print('================') rows = sheet1.row_values(1)#获取 行 内容 cols = sheet1.col_values(2)#获取 列 内容 print(rows) print(cols) print('================') print(sheet1.cell(1,0).value)#获取表格里的内容,三种方式 print(sheet1.cell_value(1,0)) print(sheet1.row(2)[0].value)