请帮我找到答案CSV文件读取并使用机器人框架从第n列中查找值我不想使用Excel库
我有 csv 文件有 5 列和 10000 行想使用机器人框架从第 3 列和 7000 行中找到价值,我将如何直接找到价值。
我在下面尝试过,我得到了值,但循环正在执行第 n 次,这很耗时,可能会产生性能问题
${value1} Create List
${FILE_CONTENT} Get File C:\abcd1.csv
Log File Content: ${FILE_CONTENT}
@{LINES} Split to Lines ${FILE_CONTENT}
Remove From List ${LINES} 0
:FOR ${LINE} IN @{LINES}
Log ${LINE}
@{ROWS} Split String ${LINE} separator=,
${VALUE} Get From List ${ROWS} 2
Log ${VALUE}
${value2} Convert To String ${VALUE}
Run Keyword If ${value2} == 1400000 Exit For Loop
Append To List ${value1} ${value2}
Log ${value1}
我按照我向您的建议尝试了内置库
github.com/s4int/robotframework-CSVLibrary
我遇到了一些错误,这可能是因为我的数据格式.csv但没有足够的时间来调试它。
我在 Python 中为您的解决方案创建了一个自定义库,您可以将其用于您的工作
数据.csv
Name,Age,region,country,Marks
pankaj,22,delhi,india,45
neeraj,32,noida,india,75
使用 CSV 模块解析此数据的 Python 代码,并返回第 n 行和第 n 列的值
import csv
#Previous function to go to nth row and nth column
def go_to_nth_row_nth_column(File,row_no,col_no):
inputFile = File
row_no=int(row_no)
col_no=int(col_no)
with open(inputFile) as ip:
reader = csv.reader(ip)
for i, row in enumerate(reader):
if i == row_no: # here's the row
#print row[col_no] # here's the column
return row[col_no]
#Function to find the string values, in case of duplicate occurrence as well
def search_cell(File,search_string):
inputFile = File
search_position=[] #empty list which will later store row,column occurences
with open(inputFile) as ip:
reader = csv.reader(ip)
for i, row in enumerate(reader):
for j, column in enumerate(row):
if search_string in column: # here's the row
#print((i,j))
search_position.append((i,j)) #this will create list of list i.e. list of row,columns in case of multi occurences
#return (i,j)
return search_position
您可以将其用作机器人文件中的库,如下所示
*** Settings ***
Library csv2.py
*** Test Cases ***
Test
Check row column
Search String
*** Keywords ***
Check row column
${result} = go_to_nth_row_nth_column data.csv 2 1
log ${result}
Search String
${result1}= search_cell data.csv india
log ${result1}
按照@pankaj的建议,使用以下代码使用库 https://github.com/s4int/robotframework-CSVLibrary。此代码查找变量 ${value_to_find}
,如果找到,则返回 继续循环并记录消息。如果要在单个列中找到关键字范围/循环verify variable
您可以自定义它。
样品机器人
*** Settings ***
Library Collections
Library CSVLibrary
*** Variables ***
${value_to_find} Morris
*** Test Cases ***
mytest
Read file and verify
*** Keywords ***
Read file and verify
@{list}= read csv file to list data.csv
:FOR ${x} IN @{list}
Log ${x}
Set test variable ${x}
verify variable
Run keyword if ${status} Log to console variable was found!
Exit For Loop If ${status}
Run keyword if not ${status} Log to console variable was not found!
verify variable
${length}= Get Length ${x}
: FOR ${j} IN RANGE 0 ${length}
Log ${x[${j}]}
${status}= Evaluate '${x[${j}]}' == '${value_to_find}'
Log ${status}
set test variable ${status}
Exit For Loop If ${status}
数据.csv
id,first_name,last_name,email,gender,ip_address
1,Douglas,Morris,dmorris0@mozilla.org,Male,205.4.212.229
2,Stephanie,Oliver,soliver1@google.com.br,Female,18.101.154.106
3,Russell,Castillo,rcastillo2@shop-pro.jp,Male,255.52.95.46
4,Helen,Reed,hreed3@rambler.ru,Female,167.55.67.109
5,Jesse,Wagner,jwagner4@histats.com,Male,252.37.62.215
6,Ashley,Diaz,adiaz5@wikia.com,Female,79.87.105.139
7,Rachel,Robinson,rrobinson6@blogger.com,Female,132.66.117.101
8,Phillip,Johnston,pjohnston7@disqus.com,Male,70.152.55.21
9,Craig,Burton,cburton8@toplist.cz,Male,73.117.157.82
10,Patrick,Fisher,pfisher9@1und1.de,Male,2.36.191.97
ID, Text, Value
20, Second Item, 200
30, Third Item, 100
40, Second Item, 300
10, First Item, 300
50, Third Item, 100
import csv
def search_cell(string):
with open("C:\abcd.csv", "rt") as f:
csvreader = csv.reader(f)
for index,row in enumerate(csvreader):
if string in row[2]:
print "300 spotted"
return row
Robot Code
${result2} search_cell 300
Log ${result2}
我只得到答案
["40"、"第二项"、"300"]我想要有 300 的列