使用Robot从txt文件中读取不同行的内容并显示(续)

本文通过创建库文件包含全局变量和子程序,优化了从txt文件读取行内容的效率。主脚本调用库文件中的ReturnRowNum获取行数,ReturnCell子程序读取指定行数据并存储于cell数组中,提高了代码的复用性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇文章中介绍了如何使用Robot编写函数,取得txt文件中每一行的内容,但给出的脚本中还存在几个问题:1.函数没有提取出来,共用性不强 2.如果想从一个有n多行的文件中把所有内容取出,则每调用一次函数,函数内部都会跑一遍循环取值,很浪费

于是我又重新整理了一下,将函数放置到库文件(.sbl)中,并在里面定一一个全局变量cell(),取值函数ReturnCell变为子程序,删除返回值的参数,这个子程序中将取得的变量放到数组cell中,这样引用这个sbl的脚本就可以使用cell()的值了。脚本如下:

主脚本:txt

'$include: "FunctionLib.sbl"

Sub main                  '主脚本
    Dim x as integer
    Dim Location as string
    Dim msgtext as string
   
    Location = "D:/gvb.txt"    '指定一个文件
   
    x = returnrownum(location)    '调用ReturnRowNum函数,返回这个文件中的行数
   
    call ReturnCell(location,x)    '调用ReturnCell,返回指定文件,指定行的内容
    msgbox cell(0)
    msgbox cell(3)

End sub

==============================================

FunctionLib.sbl

Global cell()         '全局变量,动态数组

Function ReturnRowNum(Dircetory as string) as integer    '返回指定文件的行数
   Dim testscore as String
   Dim x,i,y as integer
  
   if IsMissing(Dircetory) then
    msgbox "no Dircetory"
   End if
  
   Open Dircetory for Input as #1
   x=0
   i=0
   Do Until x=Lof(1)
      Line Input #1, testscore
      x=x+1
      i=i+1
      y=Seek(1)
      If y>Lof(1) then
         x=Lof(1)
      Else
         Seek 1,y
      End If
   Loop
   Close #1
   ReturnRowNum = i
End Function  

Sub ReturnCell(Dircetory as string,RowNum as integer)  '子程序
   Dim testscore as String
   Dim x,i,y as integer 
   if IsMissing(Dircetory) then
    msgbox "no Dircetory"
   End if
   if IsMissing(RowNum) then
    RowNum = 0
   End if
       
   Redim cell(RowNum-1)
   Open Dircetory for Input as #1
   x=0
   Do Until x=Lof(1)
      Line Input #1, testscore
      cell(x) = testscore
      x=x+1
      y=Seek(1)
      If y>Lof(1) then
         x=Lof(1)
      Else
         Seek 1,y
      End If 
   Loop
 
   Close #1
 
End Sub

 

### 使用 Robot Framework 读取 TXT 文件内容Robot Framework 中,可以通过内置的标准库 `OperatingSystem` 或者通过嵌入 Python 脚本来实现对 TXT 文件读取功能。以下是两种常见的方式: --- #### 方法一:使用 `Get File` 关键字 Robot Framework 的标准库 `OperatingSystem` 提供了一个简单的关键字 `Get File`,可以直接获取整个文件内容。 ##### 示例代码: ```robot *** Settings *** Library OperatingSystem *** Test Cases *** Read Text File Content ${file_content}= Get File path/to/your/file.txt Log File Content: ${file_content} ``` 这段代码会将指定路径下的 `file.txt` 文件内容加载到变量 `${file_content}` 中,打印出来[^1]。 --- #### 方法二:逐读取文件内容 如果需要逐处理文件内容,则可以先读取整个文件按照分割成列表形式逐一分析。 ##### 示例代码: ```robot *** Settings *** Library OperatingSystem *** Keywords *** Split File Into Lines [Arguments] ${file_path} ${content}= Get File ${file_path} @{lines}= Split To Lines ${content} [Return] ${lines} *** Test Cases *** Process Each Line In A Text File @{all_lines}= Split File Into Lines path/to/your/file.txt FOR ${line} IN @{all_lines} Log Current line: ${line} END ``` 这里定义了一个自定义关键词 `Split File Into Lines` 来返回一个由每一组成的列表,之后就可以方便地遍历这个列表对其中的数据做进一步的操作了[^2]。 --- #### 方法三:嵌入 Python 实现高级逻辑 对于更加复杂的场景比如过滤掉空白或是提取特定字段等操作时,也可以考虑直接调用 Python 原生能力来进更灵活定制化的解决方案。 ##### 示例代码: ```robot *** Settings *** Library BuiltIn *** Keywords *** Filter Non Empty Lines With Python [Arguments] ${file_path} ${filtered_lines}= Evaluate ... with open(${file_path}) as f: ... lines = [l.strip() for l in f if l.strip()] [Return] ${filtered_lines} *** Test Cases *** Use Python Code Inside RF Script ${non_empty_rows}= Filter Non Empty Lines With Python path/to/your/file.txt FOR ${row} IN ${non_empty_rows} Log Processed Row: ${row} END ``` 在这个例子当中,我们利用了 `Evaluate` 执了一段短小精悍却非常实用的小脚本完成了去除多余空格以及忽略纯空白的任务[^3]。 --- ### 总结 综上所述,无论是快速简便还是高度可控的需求都可以很好地满足。具体采用哪种方式取决于实际应用场景和个人偏好等因素综合考量决定最佳实践方案即可达成目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值