如何解决如何测试列中的数据格式-Pytest
我最近开始使用pytest测试我的python代码。 我正在尝试测试输出文件中的列应具有某种格式的值,因为不同的格式会破坏代码:
示例: 该值应为“ 5007_P2_274”,而不是“ 5007_274_P2”
我正在尝试此测试:
Val = '5007_274_P2'
try:
Val == '5007_P2_274'
except ValueError as err:
print(err)
但是我不确定这是否每次都会测试格式列的内容。值可能会有所不同,但可能会像这样:
5 * _ P * _ *
不是
5 * _ * _P *
我应该如何处理?
谢谢
解决方法
您也可以实现类似的方法。克里斯已经为您提供了一个很好的解决方案。
import re
#using regEx,you can check for the pattern
check = re.compile(r'\d{4}_P\d_\d{3}')
x = '5007_274_P2'
y = '5007_P2_274'
cx = check.search(x)==None #equating it to None results in
cy = check.search(y)==None #True if matched else False
print ('cx :',x,'is :',cx)
print ('cy :',y,cy)
输出将是:
cx : 5007_274_P2 is : True
cy : 5007_P2_274 is : False
,
from fnmatch import fnmatch
fnmatch('5007_274_P2',"5*_P*_*")
# False
fnmatch('5007_274_P2',"5*_*_P*")
# True
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。