gpt4 book ai didi

python - 不匹配时返回 NULL

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

下面的代码获取输入并在值“HttpOnly”处拆分输入,然后如果满足“if”条件,则返回该值。

如果 split() 本身条件失败,如何使值返回为 NULL 或“123”?

from soaptest.api import *
from com.parasoft.api import *

def getHeader(input, context):

headerNew = ""
strHeader = str(input).split("HttpOnly")
for i in strHeader:
if "com.abc.mb.SSO_GUID" in i:
Application.showMessage(i)
headerNew = i

return headerNew

编辑

输入 - “abcdefgHttpOnly”

输出 - “abcdefg”

输入 - “abcdefg”

输出 - “123”

最佳答案

您可以先测试“HttpOnly”是否在in 输入中,然后返回“123”。

def getHeader(input):
if 'HttpOnly' not in str(input):
return '123'

headerNew = ""
strHeader = str(input).split("HttpOnly")

# Not using i as variable since it is usually used as an index
for header in strHeader:
if "com.abc.mb.SSO_GUID" in header:
# Application.showMessage(header)
headerNew = header

return headerNew
print(getHeader('com.abc.mb.SSO_GUIDabcdefgHttpOnly')) # com.abc.mb.SSO_GUIDabcdefg
print(getHeader('com.abc.mb.SSO_GUIDabcdefg')) # 123

关于python - 不匹配时返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54946136/

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