Python 如何在Robot框架中将数据附加到csv文件?
我想将数据附加到.csv文件中,该文件在Robot框架中现在不是空的,但我遇到了一些问题 我从github中“robotframework CSVLibrary”的“s4int”安装了CSVLibrary,它有一个名为“附加到Csv文件”的关键字。我可以将数据附加到.csv文件中,但格式有一些问题 首先,我有一个空的csv文件,并在Robot框架中运行脚本Python 如何在Robot框架中将数据附加到csv文件?,python,csv,selenium,automated-tests,robotframework,Python,Csv,Selenium,Automated Tests,Robotframework,我想将数据附加到.csv文件中,该文件在Robot框架中现在不是空的,但我遇到了一些问题 我从github中“robotframework CSVLibrary”的“s4int”安装了CSVLibrary,它有一个名为“附加到Csv文件”的关键字。我可以将数据附加到.csv文件中,但格式有一些问题 首先,我有一个空的csv文件,并在Robot框架中运行脚本 *** Settings *** Library Selenium2Library Library
*** Settings ***
Library Selenium2Library
Library CSVLibrary
*** Variables ***
*** Test Cases ***
test
${list}= Create List apple pear
Append To Csv File ${file_path} ${list}
该文件如下所示:
但我预计:
!
如何添加数据以显示我所期望的内容?我的格式错了吗?或者有没有其他方法来实现这一点?
非常感谢。对于您正在使用的库,
附加到csv文件
需要列表。每个列表表示一行,每个子列表表示行中的列
由于您希望“苹果”和“梨”位于同一行,因此需要将它们放在一个列表中,然后将该列表放在另一个列表中
*** Test Cases ***
test
${list}= Create List apple pear
${data}= create list ${list}
Append To Csv File ${file_path} ${data}
如果您需要先清除该文件,然后向其中写入另一个数据,那么您可以在robot framework脚本中使用以下关键字 您可以直接使用代码,创建一个名为
CSVLibrary.py
的库,然后开始使用它。然后,您可以将其调用到脚本中。提供带有斜杠的文件路径,以便在robot框架中工作。例如:E:\\FOLDER1\\FOLDER2
。您可以使用与以前相同的方式提供数据,${list}=Create list apple pear
import csv
class CSVLibrary():
def Clear_file(self, filepath): # Clear_file :it will clear your file.
with open(filepath, 'w+') as f:
obj1=csv.writer(f)
def Append_file(self, filepath, data): # Append_file :it will append the data you wanted to in your script.
with open(filepath, 'a') as f:
obj1=csv.writer(f)
obj1.writerow(data)
使用
附加到Csv文件${File_path}@{list}
的结果是什么?对我来说,这看起来像个bug。谢谢大家。我发现使用“${list}=Create list\apple pear”可以解决这个问题。