Appium APPIUM+python 用 excel 自动读取数据的总是重复读取好几行,有大佬来看看吗

千千 · 2018年04月16日 · 最后由 ruanxiao3 回复于 2018年04月17日 · 2059 次阅读
import xlrd
def open_excel(file='file.xls'):
    try:
        data = xlrd.open_workbook(file)
        return data
    except Exception, e:
        print str(e)
    # 根据索引获取Excel表格中的数据 参数:file:Excel文件路径 colnameindex:表头列名所在行的所以 ,by_index:表的索引
def excel_table_byindex(file='file.xls', colnameindex=0, by_index=0):
    data = open_excel(file)
    table = data.sheets()[by_index]
    nrows = table.nrows  # 行数
    colnames = table.row_values(colnameindex)  # 某一行数据
    list = []
    for rownum in range(1, nrows):
        row = table.row_values(rownum)
        if row:
            app = {}
            for i in range(len(colnames)):
                app[colnames[i]] = row[i]
                list.append(app)
    return list
listdata1 = excel_table_byindex("D:\\file2.xlsx", 0)
for i in range(0, len(listdata1)):
    print(listdata1[i]['name'])


最佳回复
#-*-coding:utf-8-*-
import xlrd
def open_excel(file='file.xls'):
    try:
        data = xlrd.open_workbook(file)
        return data
    except Exception, e:
        print str(e)
    # 根据索引获取Excel表格中的数据 参数:file:Excel文件路径 colnameindex:表头列名所在行的所以 ,by_index:表的索引
def excel_table_byindex(file='file.xls', colnameindex=0, by_index=0):
    data = open_excel(file)
    table = data.sheets()[by_index]
    nrows = table.nrows  # 行数
    colnames = table.row_values(colnameindex)  # 某一行数据
    list = []
    for rownum in range(1, nrows):
        row = table.row_values(rownum)
        print row
        if row:
            app = {}
            for i in range(len(colnames)):
                app[colnames[i]] = row[i]
                print app
            # 这里从for移出来,不然重复添加了
            list.append(app)
    return list
listdata1 = excel_table_byindex("file.xls", 0)
for i in range(0, len(listdata1)):
    print(listdata1[i]['name'])
共收到 13 条回复 时间 点赞

格式太乱了,看着头晕,有 markdown 支持为啥不用呢

雨夜狂奔 回复

不好意思。。重新编辑了。。

list = []
file='file.xls'

一看这个就不想看了

hellohell 回复

大佬应该怎么写。。。。😂

hellohell 回复

可以帮萌新看看么😂 😂

#-*-coding:utf-8-*-
import xlrd
def open_excel(file='file.xls'):
    try:
        data = xlrd.open_workbook(file)
        return data
    except Exception, e:
        print str(e)
    # 根据索引获取Excel表格中的数据 参数:file:Excel文件路径 colnameindex:表头列名所在行的所以 ,by_index:表的索引
def excel_table_byindex(file='file.xls', colnameindex=0, by_index=0):
    data = open_excel(file)
    table = data.sheets()[by_index]
    nrows = table.nrows  # 行数
    colnames = table.row_values(colnameindex)  # 某一行数据
    list = []
    for rownum in range(1, nrows):
        row = table.row_values(rownum)
        print row
        if row:
            app = {}
            for i in range(len(colnames)):
                app[colnames[i]] = row[i]
                print app
            # 这里从for移出来,不然重复添加了
            list.append(app)
    return list
listdata1 = excel_table_byindex("file.xls", 0)
for i in range(0, len(listdata1)):
    print(listdata1[i]['name'])

一个简单的功能,被你写得太复杂了。。。

def ReadExcel():
    workbook = xlrd.open_workbook(config_path)

    table = workbook.sheets()[0] # 打开第一张表
    nrows = table.nrows # 获取表的行数
    # print(nrows)
    for i in range(nrows): # 循环逐行打印
        if i == 0: # 跳过第一行
            continue
        else:
            list = table.row_values(i)
            print(list)

雨夜狂奔 回复

万分感谢~~~~

KK 回复

想写脚本从 excel 提交表单。。有很多字段都在 excel 里面。才这样写。。。

千千 回复

之前我也在研究到底把测试数据放在哪;放表里?各种格式文件里?然后 由此引出了
什么测试数据分离一堆乱其八糟;你就写在测试脚本里,写在最上边作为全局变量
又能怎样,这叫模仿单元测试

for rownum in range(1, nrows):
    row = table.row_values(rownum)
    print row
    if row:
        app = {}
        for i in range(len(colnames)):
            app[colnames[i]] = row[i]
            print app
        # 这里从for移出来,不然重复添加了
        list.append(app)

这么写的不费劲吗?直接用 zip() 就可以生成带 Excel title 的字典。

for rownum in range(1, nrows):
    row = table.row_values(rownum)
    print row
    if row:
        app = dict(zip(colnames, row))
        # 这里从for移出来,不然重复添加了
        list.append(app)
莫白 回复

哈哈,其它没考虑,主要是在题主代码基础上修复他提出的问题

莫白 回复

行家里手

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册