简体   繁体   English

在文本文件中搜索首次出现的“确定”,并在同一行中提取前两个字符以将其另存为UFT / VB脚本中的变量

[英]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: 基本上,您想做的是:

  1. read the file line by line 逐行读取文件
  2. Find a substring using InStr 使用InStr查找子字符串
  3. Print the first two chars 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.

相关问题 用于读取文本文件和执行查询的 VB 脚本 - VB Scripting for reading a text file and executing query 如何在UFT中使用密码保存文件 - How to save a file with password in UFT 使用QTP / VB脚本替换已打开的文本文件中的文本 - Replace text in already opened text file using QTP/VB scripting 替换巨大的txt制表符分隔文件中第一行中的文本 - Replace text in the first line in a huge txt tab delimited file 使用 vbscript 仅删除文本文件的第一行 - Use vbscript to delete first line only of text file VB脚本+更改txt文件中第一个参数的第一个值 - VB script + change the first value of the first param in txt file VB脚本 - 比较时间值,一个来自文本文件? - VB Scripting - comparing time values, one coming from a text file? 读取 ASP-Classic 文件,但以文本格式逐行显示结果,而不是先呈现结果 - Read an ASP-Classic file , but display the results line by line in text format instead of rendering the results first VBscript:循环浏览文件夹中的所有文本文件,删除每个文件的第一行和最后一行,然后合并为一个文件 - VBscript: Loop through all text files in a folder, delete the first and last line of each file and merge into one file 将每一行文本(加上第一行)输出到其自己的.txt文件VBS-Script - Output each line of text (+ the first line) to its own .txt file VBS-Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM