如果标签中不存在元素,则打印"E;”python编程
编写的代码:如果标签中不存在元素,则打印"E;”python编程,python,Python,编写的代码: s = ''' :20:name :21A:Address :22B:phone :57A:/256789422254 TEST VALUE :59B:/REST_MA LINE1 :59C:TEST ''' 结果显示: A的值:256789 B值:试验值 If:57A:没有两行 例: 然后我希望输出为 预期: A的值:256789 B的值:“” 实际值: A的值:256789 B的值::59B:/REST\u MA 因为A和B的值应来自:57A: 但从输出中,它会显示:59B:
s = '''
:20:name
:21A:Address
:22B:phone
:57A:/256789422254
TEST VALUE
:59B:/REST_MA
LINE1
:59C:TEST
'''
结果显示:
A的值:256789B值:试验值 If:57A:没有两行 例: 然后我希望输出为 预期:
A的值:256789
B的值:“” 实际值:
A的值:256789
B的值::59B:/REST\u MA 因为A和B的值应来自:57A:
但从输出中,它会显示:59B:检查以b中的
:
开头的标记
s = '''
:20:name
:21A:Address
:22B:phone
:57A:/256789422254
:59B:/REST_MA
LINE1
:59C:TEST
'''
检查变量“b”是否以冒号开头,以验证它是新字段还是57A字段的延续
if ':57A:' in s:
a, b = s[s.index(':57A:') - 1 :].strip().split("\n")[:2]
a = a.split(':')[2]
print("value of A:" + a[1:7])
if b.startswith(':'): # add this check
b = ''
print("value of B:" + b)
else:
print('not found')
if ':57A:' in s:
a, b = s[s.index(':57A:') - 1 :].strip().split("\n")[:2]
a = a.split(':')[2]
print("value of A:" + a[1:7])
if b.startswith(':'): # add this check
b = ''
print("value of B:" + b)
else:
print('not found')
if ':57A:' in s:
a, b = s[s.index(':57A:') - 1 :].strip().split("\n")[:2]
a = a.split(':')[2]
print("value of A:" + a[1:7])
if b.startswith(":"):
print("value of B: ''")
else:
print("value of B:" + b)
else:
print('not found')