0
def c1():
logfile = open("D:\myfile.txt", 'r')
for num1, line in enumerate(logfile):
if "request=100" in line:
print num1
return True
return False
def c2():
logfile = open("D:\myfile.txt", 'r')
for num2, line in enumerate(logfile):
if "response=200" in line:
print num2
return True
return False
if c1() == True and c2() == True:
print "Test Case Passed"
else:
print "Test Case Failed"
在上面的代碼中,檢查行號以便request = 100和response = 200將落在同一行中不存在。我需要。查找在Python中搜索的字符串的行號
另外,我想打印結果爲「合格」只有滿足以下條件......
- both c1 and c2 are True
- both "request=100" and "response=200" should fall in same line
- if any other line also consist of "request=100" and "response=200" then that also should be counted
結果是「失敗」,如果:
- if any one line which consists of "request=200" and "response=200"
- if any one line which consists of "request=100" and "response=100"
- or any case in which no line should have apart from "request=100" and "response=200"
考慮「MYFILE 「有以下數據:
request=100 XXXXXXXXXXXXXXXXXXXXXXXXXXXX \n
XXXXXXXXXXXXXXXX response=200 XXXXXXXXX \n
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \n
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \n
XXXX request=100 XXXXX response=200 XXXXXXXXXXX \n
XXXXXXX request=200 XXXXXX response=100 XXXXXXX \n
XXXXXXXX request=100 XXXX response=100" \n
在上面的文件中,結果是失敗的,因爲請求和響應具有不同的值除了要求的值之外,只有第5行具有正確的值,因此結果失敗。