【发布时间】:2016-11-10 21:12:00
【问题描述】:
我在 python 中编写了这段代码来读取 CSV 文件中的一行:
import csv
class CsvLibrary(object):
ROBOT_LIBRARY_SCOPE = 'Global'
def __init__(self):
print 'Read Cell Value in CSV File'
def read_cell_value_by_row(self,filename,rownumber):
#read an existing csv file
userdata=[]
user={}
with open(filename,'rb') as csvfile:
#read csv file as dictionary object
reader=csv.DictReader(csvfile)
#read and store it as list of dictionary items
userdata=list(reader)
#get specific list item as a dictionary object
user=userdata[rownumber-1]
#return the dictionary object
return user
然后在robotframework中我做这个脚本:
${row_number}= Convert To Integer 1
${user_info}= read_cell_value_by_row UserInfo.csv ${row_number}
${username} Get From Dictionary ${user_info} username
Log ${username}
${password} Get From Dictionary ${user_info} password
Log ${password}
Input Text ${login_username_textbox} ${username}
Input Text ${login_password_textbox} ${password}
但我不知道如何读取 CSV 文件中的随机行,有人可以帮助我吗?
【问题讨论】:
-
在同一个python文件中,干脆写一个“get_random row”方法,可以作为keyword_使用?生成一个介于零和数据长度之间的随机数,并将其用作返回行的索引。
标签: csv robotframework