gpt4 book ai didi

python - excel 文件处理 xlrd 期间的 pytest 警告

转载 作者:太空宇宙 更新时间:2023-11-03 21:22:57 26 4
gpt4 key购买 nike

我创建了一个简单的 pytest 示例,其中我打开一个 Excel 文件并计算工作表的数量并对其进行断言。

测试用例通过了,但不知何故我收到了 14 条警告。如何删除这些警告?

设置:包含 6 个工作表的 test.xlsx 文件。

test_main.py

import xlrd


# ----------------------------------------------------------------------
def test_open_file():
"""
Open and read an Excel file
"""
path = "test.xlsx"
book = xlrd.open_workbook(path)

# print number of sheets
print(book.nsheets)

assert book.nsheets == 6



# ----------------------------------------------------------------------
if __name__ == "__main__":
test_open_file()

然后我运行命令 pytest -v。

结果:

============================================================================= test session starts ==============================================================================
platform win32 -- Python 3.7.0, pytest-4.1.0, py-1.7.0, pluggy-0.8.0 -- c:\users\testuser\appdata\local\programs\python\python37\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\testuser\PycharmProjects\testingpytest\testing, inifile:
collected 1 item

test_main.py::test_open_file PASSED [100%]

=============================================================================== warnings summary ===============================================================================
test_main.py::test_open_file
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\defusedxml\ElementTree.py:68: DeprecationWarning: The html argument of XMLParser() is deprecated
_XMLParser.__init__(self, html, target, encoding)
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\defusedxml\ElementTree.py:68: DeprecationWarning: The html argument of XMLParser() is deprecated
_XMLParser.__init__(self, html, target, encoding)
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\defusedxml\ElementTree.py:68: DeprecationWarning: The html argument of XMLParser() is deprecated
_XMLParser.__init__(self, html, target, encoding)
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\xlrd\xlsx.py:266: PendingDeprecationWarning: This method will be removed in future versions. Use 'tr
ee.iter()' or 'list(tree.iter())' instead.
for elem in self.tree.iter() if Element_has_iter else self.tree.getiterator():
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\defusedxml\ElementTree.py:68: DeprecationWarning: The html argument of XMLParser() is deprecated
_XMLParser.__init__(self, html, target, encoding)
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\xlrd\xlsx.py:312: PendingDeprecationWarning: This method will be removed in future versions. Use 'tr
ee.iter()' or 'list(tree.iter())' instead.
for elem in self.tree.iter() if Element_has_iter else self.tree.getiterator():
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\defusedxml\ElementTree.py:68: DeprecationWarning: The html argument of XMLParser() is deprecated
_XMLParser.__init__(self, html, target, encoding)
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\xlrd\xlsx.py:266: PendingDeprecationWarning: This method will be removed in future versions. Use 'tr
ee.iter()' or 'list(tree.iter())' instead.
for elem in self.tree.iter() if Element_has_iter else self.tree.getiterator():
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\defusedxml\ElementTree.py:68: DeprecationWarning: The html argument of XMLParser() is deprecated
_XMLParser.__init__(self, html, target, encoding)
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\defusedxml\ElementTree.py:68: DeprecationWarning: The html argument of XMLParser() is deprecated
_XMLParser.__init__(self, html, target, encoding)
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\defusedxml\ElementTree.py:68: DeprecationWarning: The html argument of XMLParser() is deprecated
_XMLParser.__init__(self, html, target, encoding)
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\defusedxml\ElementTree.py:68: DeprecationWarning: The html argument of XMLParser() is deprecated
_XMLParser.__init__(self, html, target, encoding)
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\defusedxml\ElementTree.py:68: DeprecationWarning: The html argument of XMLParser() is deprecated
_XMLParser.__init__(self, html, target, encoding)
c:\users\testuser\appdata\local\programs\python\python37\lib\site-packages\defusedxml\ElementTree.py:68: DeprecationWarning: The html argument of XMLParser() is deprecated
_XMLParser.__init__(self, html, target, encoding)

-- Docs: https://docs.pytest.org/en/latest/warnings.html
==================================================================== 1 passed, 14 warnings in 0.13 seconds =====================================================================

最佳答案

您可以暂时忽略警告(导入警告模块后):

with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
warnings.filterwarnings("ignore", category=DeprecationWarning)
book = xlrd.open_workbook(path)

关于python - excel 文件处理 xlrd 期间的 pytest 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54098639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com