[英]Search first occurance of “OK” in a text file and extract first 2 characters in the same line to save it as a variable in UFT/VB scripting
I've been trying to find a proper solution to my problem for several days now looking everywhere. 我一直在寻找适合我的问题的解决方案已有好几天了,现在到处都是。 Hopefully some of you guys can direct me to the right direction.
希望你们中的一些人可以指引我正确的方向。
I need to find the string "OK" in a text file and Extract first 2 characters in the same line if I find "OK" to save it as a variable. 我需要在文本文件中找到字符串“ OK”,如果找到“ OK”将其另存为变量,请在同一行中提取前2个字符。
I give you an example of the lines you can find in this text file: 我为您提供了一个在此文本文件中可以找到的行的示例:
Debugger
--------------
>h state 2
Health thread state is: POLLING
Health Devices:
Sensor Name State Eval RED Value ( D , M ) Link Active Grp Description
11 ( 2) TEMP ( 1) OK 1 1 21 ( 1, 0) 0xff 0000 0 01-Inlet Ambient (X:1 y:1)
12 ( 2) TEMP ( 1) OK 0 1 40 ( 1, 0) 0xff 0000 0 02-CPU 1 (X:11 y:5)
13 ( 2) TEMP ( 2) MISSING 0 1 0 ( 0, 1) 0xff 0000 0 04-P1 DIMM 1-6 (X:14 y:5)
14 ( 2) TEMP ( 1) OK 0 1 24 ( 1, 0) 0xff r0000 0 05-P1 DIMM 7-12 (X:9 y:5)
15 ( 2) TEMP ( 2) MISSING 0 1 0 ( 0, 1) 0xff 0000 0 06-P2 DIMM 1-6 (X:6 y:5)
16 ( 2) TEMP ( 2) MISSING 0 1 0 ( 0, 0) 0xff 0000 0 07-P2 DIMM 7-12 (X:1 y:5)
17 ( 2) TEMP ( 1) OK 0 1 35 ( 1, 0) 0xff 0000 0 08-HD Max (X:2 y:3)
18 ( 2) TEMP ( 1) OK 0 1 38 ( 1, 0) 0xff 0000 0 10-Chipset (X:13 y:10)
19 ( 2) TEMP ( 1) OK 0 1 24 ( 1, 0) 0xff 0000 0 11-PS 1 Inlet (X:1 y:14)
20 ( 2) TEMP ( 2) MISSING 0 1 0 ( 0, 0) 0xff 0000 0 12-PS 2 Inlet (X:4 y:14)
21 ( 2) TEMP ( 1) OK 0 1 32 ( 1, 0) 0xff 0000 0 13-VR P1 (X:10 y:1)
22 ( 2) TEMP ( 1) OK 0 1 28 ( 1, 0) 0xff 0000 0 15-VR P1 Mem (X:13 y:1)
23 ( 2) TEMP ( 1) OK 0 1 27 ( 1, 0) 0xff 0000 0 16-VR P1 Mem (X:9 y:1)
24 ( 2) TEMP ( 1) OK 0 1 40 ( 1, 0) 0xff 0000 0 19-PS 1 Internal (X:8 y:1)
25 ( 2) TEMP ( 2) MISSING 0 1 0 ( 0, 0) 0xff 0000 0 20-PS 2 Internal (X:1 y:8)
26 ( 2) TEMP ( 2) MISSING 0 1 0 ( 0, 0) 0xff 0000 0 21-PCI 1 (X:5 y:12)
27 ( 2) TEMP ( 2) MISSING 0 1 0 ( 0, 0) 0xff 0000 0 22-PCI 2 (X:11 y:12)
28 ( 2) TEMP ( 2) MISSING 0 1 0 ( 0, 0) 0xff 0000 0
Using the following Code I can extract the first 2 characters in a line but I have to extract 2 characters from the line where I find the first occurrence of "OK" 使用以下代码,我可以提取一行中的前2个字符,但是我必须从找到“ OK”的第一个字符的行中提取2个字符
strLine = objTextFile.ReadLine objTextFile.Close 'Gets first 2 chars SerNum = Left(strLine, 2)
Looking for help in this... Thanks in advance... 在此寻求帮助...预先感谢...
My unfinished vbscript: 我未完成的vbscript:
Const ForReading = 1 Dim strSearchFor strSearchFor = "OK" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile("C:\\myFile.txt", ForReading) For i = 0 to 20 strLine = objTextFile.ReadLine() If InStr(strLine, strSearchFor) > 0 Then SensorNumb = Left(strLine, 2) Exit For End If Next
Final Code : 最终代码:
Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile("C:\\myFile.txt", ForReading) For i = 0 to 20 strLine = objTextFile.ReadLine() If InStr(strLine, "OK") > 0 Then SensorNumb = Left(strLine, 2) objTextFile.Close Exit For End If Next
This python script should work for you: 这个python脚本应该为您工作:
lines = [line[:-1] for line in open("MY_FILENAME")]
for i in range(len(lines)):
if lines[i].contains("OK"):
print lines[i][:2]
Save this as a *.py
file and execute it with python path/to/python/file
将其另存为
*.py
文件,并使用python path/to/python/file
Basically what you want to do is: 基本上,您想做的是:
InStr
InStr
查找子字符串 Mid(str, 1, 2)
Mid(str, 1, 2)
You should be able to chain these together yourself. 您应该可以自己将它们链接在一起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.