2016-01-20 764 views

回答

5

从OperatingSystem的图书馆您使用Get File可以使用Create File从OperatingSystem的库读取文件的内容,
使用Replace String从字符串库,你可以替换字符串和 您可以创建文件。

0

如果您想要替换的文件很大并且您不想将整个文件内容加载到变量中,则可以使用OperatingSystem Library中的Run关键字。关键字将执行shell命令,在这里它是“sed”并进行替换。 (当然,你应该在Linux上在这种情况下)

例如:

*** Settings *** 
| Library | OperatingSystem 

*** Test Cases *** 
| Example of replacing the text in a file 
| | ${result}= | Run | sed -i 's/foo/bar/g' myfile.txt 

变量result将持有你在运行shell命令后,即可获取标准输出。

0

我为此使用了python脚本。将下面的代码放在.py文件中,并使用库中的设置部分将其加载到机械手文件中。 接下来在机器人文件中使用此关键字:

替换文件中的行|文件| searchExp1 | replaceLine

def Replace_line_in_file(file,searchExp1,replaceLine): 
    """ Open a file (like input.txt) and find the line that 
     contains the string searchExp1. 
     and replace that complete line by replaceLine. 
     When there are multiple lines that contain searchExp1 
     then all those lines will be replaced 
    """ 
    for line in fileinput.input(file, inplace=1): 
     if searchExp1 in line: 
      line = replaceLine+'\n' 
     sys.stdout.write(line)