帮我看一下怎么打印这个方法返回的列表值 import xlrd "打开文件" def open_excel(): testcases=[] wb = xlrd.open_workbook('G:\\接口测试框架\\testpytest\data\\接口测试用例.xls') """ print('sheet的名称:', sheet.name) print('sheet的总行数:', sheet.nrows) print('sheet的总列数:', sheet.ncols) print('接口文档地址:', sheet.row_values(1)[0]) print('API地址:', sheet.row_values(2)) print('用例标题: ',sheet.row_values(3) ) """ sheet = wb.sheet_by_name('论坛接口') rownum = int(sheet.nrows) #print('接口测试用例:',sheet.cell_value()) for i in range(1,rownum): hh=dict(zip(sheet.row_values(0),sheet.row_values(i))) return testcases.append(hh) if __name__ =='__main__': open_excel() print()
时间: 2024-02-07 18:03:51 浏览: 34
在这个代码中,你定义了一个名为 `open_excel` 的方法,这个方法会打开一个 Excel 文件,然后读取名为 `论坛接口` 的工作表中的数据,并将每一行的数据转换为字典格式,最后将字典添加到一个名为 `testcases` 的列表中。但是,你的代码有一个小问题,就是在 `return` 语句中,你应该先将字典添加到列表中,然后再返回列表。下面是修改后的代码:
```python
import xlrd
def open_excel():
testcases = []
wb = xlrd.open_workbook('G:\\接口测试框架\\testpytest\data\\接口测试用例.xls')
sheet = wb.sheet_by_name('论坛接口')
rownum = int(sheet.nrows)
for i in range(1, rownum):
hh = dict(zip(sheet.row_values(0), sheet.row_values(i)))
testcases.append(hh)
return testcases
if __name__ == '__main__':
testcases = open_excel()
print(testcases)
```
你可以直接调用 `open_excel` 函数,并将返回的 `testcases` 列表打印出来。
相关问题
优化下这段代码 import xdrlib, sys import xlrd import matplotlib.pyplot as plt class ReadExcel: def __init__(self, file, sheet_name, g_ranklist, ranklist, ranklist2): self.file = file self.sheet_name = sheet_name self.colnameindex =
以下是对代码进行优化的建议:
1. 删除没有使用的导入模块 `xdrlib` 和 `matplotlib.pyplot`。
2. 将类中的构造函数 `__init__` 拆分为两个独立的方法,一个用于读取 Excel 文件并获取工作表,另一个用于处理工作表数据。
3. 通过添加 `try-except` 块来捕获可能的异常情况,例如文件不存在或工作表名称错误。
4. 将变量名称更改为更具描述性的名称,以提高代码的可读性。
重构后的代码如下所示:
```python
import xlrd
class ReadExcel:
def __init__(self, file_path, sheet_name):
self.file_path = file_path
self.sheet_name = sheet_name
self.colnameindex = 0
def get_sheet(self):
try:
workbook = xlrd.open_workbook(self.file_path)
sheet = workbook.sheet_by_name(self.sheet_name)
return sheet
except FileNotFoundError:
print("File not found.")
return None
except xlrd.biffh.XLRDError:
print("Sheet not found.")
return None
def process_data(self, g_rank_list, rank_list, rank_list2):
sheet = self.get_sheet()
if sheet:
num_rows = sheet.nrows
for curr_row in range(1, num_rows):
row = sheet.row_values(curr_row)
g_rank_list.append(int(row[0]))
rank_list.append(int(row[1]))
rank_list2.append(int(row[2]))
```
在上面的代码中,我们首先删除了导入模块 `xdrlib` 和 `matplotlib.pyplot`,因为它们没有在代码中使用。然后,我们将构造函数 `__init__` 拆分为两个独立的方法 `get_sheet` 和 `process_data`。方法 `get_sheet` 用于打开 Excel 文件并获取指定的工作表,而方法 `process_data` 用于处理工作表数据,并将其存储在给定的列表中。我们还使用 `try-except` 块来捕获可能的异常情况,并更改了变量名称以提高代码的可读性。
帮我重写如下python代码,#coding=utf-8-sig import re import os import xlrd import xlwt import time import datetime import math #import pandas import sys import shutil #通用部分 def time_mc(number_t): localTime = time.strftime("%Y%m%d%H%M%S", time.localtime()) time_mc_n
当然可以,以下是重写后的代码:
```python
#coding=utf-8-sig
import re
import os
import xlrd
import xlwt
import time
import datetime
import math
import sys
import shutil
# 通用部分
def time_mc(number_t):
localTime = time.strftime("%Y%m%d%H%M%S", time.localtime())
time_mc_n = localTime + str(number_t)
return time_mc_n
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
def del_file(path):
if os.path.exists(path):
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
def get_file_path(root_path, file_type):
file_list = []
for dirpath, dirnames, filenames in os.walk(root_path):
for filename in filenames:
if filename.endswith(file_type):
file_list.append(os.path.join(dirpath, filename))
return file_list
# 主程序
if __name__ == '__main__':
pass
```
我将原来的代码中的注释进行了调整,并且将一些通用函数进行了函数封装,以方便代码的复用性和可读性。