无法停止显示弃用警告

时间:2019-05-23 07:20:50

标签: python python-3.x

我正在尝试从Excel文件中读取值,当我这样做时:

for i in range(1, row_num + 1): 
        try:
            cell_obj = sheet.cell(row = i, column = 1) 

我收到以下警告:

DeprecationWarning: Call to deprecated function get_squared_range (Use ws.iter_rows()).
  for row in self.get_squared_range(column, row, column, row):

我已经尝试实现Python文档中所说的内容,但是它不起作用或者我没有正确使用它,我还尝试执行以下操作:

for i in range(1, row_num + 1): 
        try:
            cell_obj = sheet.cell(row = i, column = 1) 
            myList.append((cell_obj).value)   
            warnings.filterwarnings("ignore", category=DeprecationWarning)

但同样,什么也没有发生。谁能对此有所启发?

2 个答案:

答案 0 :(得分:2)

如警告所示,您可以使用app.module.ts方法来迭代单元格。

以下是使用iter_rows存储前五行的单元格值的示例。

iter_rows

输出:

from openpyxl import load_workbook
wb = load_workbook(filename = 'data.xlsx')
sheet = wb['Sheet1']
number_of_rows = sheet.max_row
number_of_columns = sheet.max_column

# Store values from first five rows
rows_iter = sheet.iter_rows(min_col=1, min_row=1,
                            max_col=number_of_columns, max_row=5)
values = [[cell.value for cell in row] for row in rows_iter]
print(values)

我已将Sample Excel Spreadsheet provided by Microsoft用作[['Segment', 'Country', 'Product', 'Discount Band', 'Units Sold', 'Manufacturing Price', 'Sale Price', 'Gross Sales', 'Discounts', ' Sales', 'COGS', 'Profit', 'Date', 'Month Number', 'Month Name', 'Year'], ['Government', 'Canada', 'Carretera', 'None', 1618.5, 3, 20, 32370, 0, 32370, 16185, 16185, datetime.datetime(2014, 1, 1, 0, 0), 1, 'January', '2014'], ['Government', 'Germany', 'Carretera', 'None', 1321, 3, 20, 26420, 0, 26420, 13210, 13210, datetime.datetime(2014, 1, 1, 0, 0), 1, 'January', '2014'], ['Midmarket', 'France', 'Carretera', 'None', 2178, 3, 15, 32670, 0, 32670, 21780, 10890, datetime.datetime(2014, 6, 1, 0, 0), 6, 'June', '2014'], ['Midmarket', 'Germany', 'Carretera', 'None', 888, 3, 15, 13320, 0, 13320, 8880, 4440, datetime.datetime(2014, 6, 1, 0, 0), 6, 'June', '2014']]

已安装的软件包:

data.xlsx

答案 1 :(得分:1)

由于Python是一种迭代语言,因此语句将一一执行。

尝试发表以下声明

$absPath = getcwd(); $filename = str_replace("\\administrator","",$absPath) .$filename;

之前

urllib.urlopen

建议在全局空间中使用警告过滤器语句,因为局部禁止警告不会反映最初的意图。