Skip to content

Commit d4957d9

Browse files
committed
Support for reading selected lines from file
1 parent 2661ce0 commit d4957d9

4 files changed

Lines changed: 29 additions & 6 deletions

File tree

CSVLibrary/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ def _open_csv_file_for_read(filename, csv_reader=csv.reader, line_numbers=None,
1414
with open(filename, 'r') as csv_handler:
1515
reader = csv_reader(csv_handler, **kwargs)
1616
try:
17-
for row in reader:
18-
yield row
17+
for line_number, row in enumerate(reader):
18+
if line_numbers is None:
19+
yield row
20+
elif isinstance(line_numbers, list):
21+
if line_number in line_numbers:
22+
yield row
23+
line_numbers.remove(line_number)
24+
if len(line_numbers) == 0:
25+
break
1926
except csv.Error as e:
2027
logger.error('file %s, line %d: %s' % (filename, reader.line_num, e))
2128

@@ -46,6 +53,7 @@ def read_csv_file_to_list(self, filename, delimiter=',', **kwargs):
4653
4754
- ``filename``: name of csv file
4855
- ``delimiter``: Default: `,`
56+
- ``line_numbers``: List of linenumbers to read. Default None
4957
- ``quoting`` (int):
5058
_0_: QUOTE_MINIMAL
5159
_1_: QUOTE_ALL
@@ -66,12 +74,12 @@ def read_csv_file_to_associative(self, filename, delimiter=',', fieldnames=None,
6674
- ``filename``: name of csv file
6775
- ``delimiter``: Default: `,`
6876
- ``fieldnames``: list of column names
77+
- ``line_numbers``: List of linenumbers to read. Default None
6978
- ``quoting`` (int):
7079
_0_: QUOTE_MINIMAL
7180
_1_: QUOTE_ALL
7281
_2_: QUOTE_NONNUMERIC
7382
_3_: QUOTE_NONE
74-
7583
"""
7684
csv_dict = self._open_csv_file_for_read(
7785
filename,

Examples/CSVExampleTest.robot

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
*** Settings ***
22
Documentation CSV examples for Robot Framework.
33
Library Collections
4-
Library CSVLibrary
4+
Library ${CURDIR}${/}..${/}CSVLibrary
55

66
*** Variables ***
77
# Example data generated with: https://www.mockaroo.com/
88
@{template_list}= 1 Douglas Morris dmorris0@mozilla.org Male 205.4.212.229
99
&{template_dict}= id=1 first_name=Douglas last_name=Morris email=dmorris0@mozilla.org gender=Male ip_address=205.4.212.229
10-
10+
&{template_dict_quoting}= id=1 first_name=Douglas last_name=Morris email=dmorris0@mozilla.org gender="Male ip_address=205.4.212.229
1111

1212
*** Test Cases ***
1313
Read csv file to a list example test
@@ -17,3 +17,7 @@ Read csv file to a list example test
1717
Read csv file to a dict example test
1818
@{dict}= read csv file to associative ${CURDIR}${/}data.csv
1919
dictionaries should be equal ${template_dict} ${dict[0]}
20+
21+
Read csv file without quoting to associative
22+
@{dict}= read csv file to associative ${CURDIR}${/}data_quoting.csv delimiter=, quoting=${3}
23+
dictionaries should be equal ${template_dict_quoting} ${dict[0]}

Examples/data_quoting.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id,first_name,last_name,email,gender,ip_address
2+
1,Douglas,Morris,dmorris0@mozilla.org,"Male,205.4.212.229
3+
2,Stephanie,Oliver,soliver1@google.com.br,Female,18.101.154.106
4+
3,Russell,Castillo,rcastillo2@shop-pro.jp,Male,255.52.95.46
5+
4,Helen,Reed,hreed3@rambler.ru,Female,167.55.67.109
6+
5,Jesse,Wagner,jwagner4@histats.com,Male,252.37.62.215
7+
6,Ashley,Diaz,adiaz5@wikia.com,Female,79.87.105.139
8+
7,Rachel,Robinson,rrobinson6@blogger.com,Female,132.66.117.101
9+
8,Phillip,Johnston,pjohnston7@disqus.com,Male,70.152.55.21
10+
9,Craig,Burton,cburton8@toplist.cz,Male,73.117.157.82
11+
10,Patrick,Fisher,pfisher9@1und1.de,Male,2.36.191.97

doc/CSVLibrary.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@
547547
jQuery.extend({highlight:function(e,t,n,r){if(e.nodeType===3){var i=e.data.match(t);if(i){var s=document.createElement(n||"span");s.className=r||"highlight";var o=e.splitText(i.index);o.splitText(i[0].length);var u=o.cloneNode(true);s.appendChild(u);o.parentNode.replaceChild(s,o);return 1}}else if(e.nodeType===1&&e.childNodes&&!/(script|style)/i.test(e.tagName)&&!(e.tagName===n.toUpperCase()&&e.className===r)){for(var a=0;a<e.childNodes.length;a++){a+=jQuery.highlight(e.childNodes[a],t,n,r)}}return 0}});jQuery.fn.unhighlight=function(e){var t={className:"highlight",element:"span"};jQuery.extend(t,e);return this.find(t.element+"."+t.className).each(function(){var e=this.parentNode;e.replaceChild(this.firstChild,this);e.normalize()}).end()};jQuery.fn.highlight=function(e,t){var n={className:"highlight",element:"span",caseSensitive:false,wordsOnly:false};jQuery.extend(n,t);if(e.constructor===String){e=[e]}e=jQuery.grep(e,function(e,t){return e!=""});e=jQuery.map(e,function(e,t){return e.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")});if(e.length==0){return this}var r=n.caseSensitive?"":"i";var i="("+e.join("|")+")";if(n.wordsOnly){i="\\b"+i+"\\b"}var s=new RegExp(i,r);return this.each(function(){jQuery.highlight(this,s,n.element,n.className)})}
548548
</script>
549549
<script type="text/javascript">
550-
libdoc = {"all_tags":[],"contains_tags":false,"doc":"<p>Documentation for test library <code>CSVLibrary\x3c/code>.\x3c/p>","generated":"2017-04-06 20:56:18","inits":[],"keywords":[{"args":["filename","data","**kwargs"],"doc":"<p>This keyword will append data to a new or existing CSV file.\x3c/p>\n<ul>\n<li><code>filename\x3c/code>: name of csv file\x3c/li>\n<li><code>data\x3c/code>: iterable(e.g. list or tuple) data.\x3c/li>\n<li><code>quoting\x3c/code> (int): <i>0\x3c/i>: QUOTE_MINIMAL <i>1\x3c/i>: QUOTE_ALL <i>2\x3c/i>: QUOTE_NONNUMERIC <i>3\x3c/i>: QUOTE_NONE\x3c/li>\n\x3c/ul>","matched":true,"name":"Append To Csv File","shortdoc":"This keyword will append data to a new or existing CSV file.","tags":[]},{"args":["filename","data","fieldnames=None","delimiter=,","**kwargs"],"doc":"<p>This keyword will create new file\x3c/p>\n<ul>\n<li><code>filename\x3c/code>: name of csv file\x3c/li>\n<li><code>data\x3c/code>: iterable(e.g. list or tuple) data.\x3c/li>\n<li><code>fieldnames\x3c/code>: list of column names\x3c/li>\n<li><code>delimiter\x3c/code>: Default: <span class=\"name\">,\x3c/span>\x3c/li>\n<li><code>quoting\x3c/code> (int): <i>0\x3c/i>: QUOTE_MINIMAL <i>1\x3c/i>: QUOTE_ALL <i>2\x3c/i>: QUOTE_NONNUMERIC <i>3\x3c/i>: QUOTE_NONE\x3c/li>\n\x3c/ul>","matched":true,"name":"Csv File From Associative","shortdoc":"This keyword will create new file","tags":[]},{"args":["filename"],"doc":"<p>This keyword will empty the CSV file.\x3c/p>\n<ul>\n<li><code>filename\x3c/code>: name of csv file\x3c/li>\n\x3c/ul>","matched":true,"name":"Empty Csv File","shortdoc":"This keyword will empty the CSV file.","tags":[]},{"args":["filename","delimiter=,","fieldnames=None","**kwargs"],"doc":"<p>Read CSV file and return its content as a Python list of dictionaries.\x3c/p>\n<ul>\n<li><code>filename\x3c/code>: name of csv file\x3c/li>\n<li><code>delimiter\x3c/code>: Default: <span class=\"name\">,\x3c/span>\x3c/li>\n<li><code>fieldnames\x3c/code>: list of column names\x3c/li>\n<li><code>quoting\x3c/code> (int): <i>0\x3c/i>: QUOTE_MINIMAL <i>1\x3c/i>: QUOTE_ALL <i>2\x3c/i>: QUOTE_NONNUMERIC <i>3\x3c/i>: QUOTE_NONE\x3c/li>\n\x3c/ul>","matched":true,"name":"Read Csv File To Associative","shortdoc":"Read CSV file and return its content as a Python list of dictionaries.","tags":[]},{"args":["filename","delimiter=,","**kwargs"],"doc":"<p>Read CSV file and return its content as a Python list of tuples.\x3c/p>\n<ul>\n<li><code>filename\x3c/code>: name of csv file\x3c/li>\n<li><code>delimiter\x3c/code>: Default: <span class=\"name\">,\x3c/span>\x3c/li>\n<li><code>quoting\x3c/code> (int): <i>0\x3c/i>: QUOTE_MINIMAL <i>1\x3c/i>: QUOTE_ALL <i>2\x3c/i>: QUOTE_NONNUMERIC <i>3\x3c/i>: QUOTE_NONE\x3c/li>\n\x3c/ul>","matched":true,"name":"Read Csv File To List","shortdoc":"Read CSV file and return its content as a Python list of tuples.","tags":[]}],"name":"CSVLibrary","named_args":true,"scope":"test case","version":""};
550+
libdoc = {"all_tags":[],"contains_tags":false,"doc":"<p>Documentation for test library <code>CSVLibrary\x3c/code>.\x3c/p>","generated":"2017-04-06 21:03:17","inits":[],"keywords":[{"args":["filename","data","**kwargs"],"doc":"<p>This keyword will append data to a new or existing CSV file.\x3c/p>\n<ul>\n<li><code>filename\x3c/code>: name of csv file\x3c/li>\n<li><code>data\x3c/code>: iterable(e.g. list or tuple) data.\x3c/li>\n<li><code>quoting\x3c/code> (int): <i>0\x3c/i>: QUOTE_MINIMAL <i>1\x3c/i>: QUOTE_ALL <i>2\x3c/i>: QUOTE_NONNUMERIC <i>3\x3c/i>: QUOTE_NONE\x3c/li>\n\x3c/ul>","matched":true,"name":"Append To Csv File","shortdoc":"This keyword will append data to a new or existing CSV file.","tags":[]},{"args":["filename","data","fieldnames=None","delimiter=,","**kwargs"],"doc":"<p>This keyword will create new file\x3c/p>\n<ul>\n<li><code>filename\x3c/code>: name of csv file\x3c/li>\n<li><code>data\x3c/code>: iterable(e.g. list or tuple) data.\x3c/li>\n<li><code>fieldnames\x3c/code>: list of column names\x3c/li>\n<li><code>delimiter\x3c/code>: Default: <span class=\"name\">,\x3c/span>\x3c/li>\n<li><code>quoting\x3c/code> (int): <i>0\x3c/i>: QUOTE_MINIMAL <i>1\x3c/i>: QUOTE_ALL <i>2\x3c/i>: QUOTE_NONNUMERIC <i>3\x3c/i>: QUOTE_NONE\x3c/li>\n\x3c/ul>","matched":true,"name":"Csv File From Associative","shortdoc":"This keyword will create new file","tags":[]},{"args":["filename"],"doc":"<p>This keyword will empty the CSV file.\x3c/p>\n<ul>\n<li><code>filename\x3c/code>: name of csv file\x3c/li>\n\x3c/ul>","matched":true,"name":"Empty Csv File","shortdoc":"This keyword will empty the CSV file.","tags":[]},{"args":["filename","delimiter=,","fieldnames=None","**kwargs"],"doc":"<p>Read CSV file and return its content as a Python list of dictionaries.\x3c/p>\n<ul>\n<li><code>filename\x3c/code>: name of csv file\x3c/li>\n<li><code>delimiter\x3c/code>: Default: <span class=\"name\">,\x3c/span>\x3c/li>\n<li><code>fieldnames\x3c/code>: list of column names\x3c/li>\n<li><code>line_numbers\x3c/code>: List of linenumbers to read. Default None\x3c/li>\n<li><code>quoting\x3c/code> (int): <i>0\x3c/i>: QUOTE_MINIMAL <i>1\x3c/i>: QUOTE_ALL <i>2\x3c/i>: QUOTE_NONNUMERIC <i>3\x3c/i>: QUOTE_NONE\x3c/li>\n\x3c/ul>","matched":true,"name":"Read Csv File To Associative","shortdoc":"Read CSV file and return its content as a Python list of dictionaries.","tags":[]},{"args":["filename","delimiter=,","**kwargs"],"doc":"<p>Read CSV file and return its content as a Python list of tuples.\x3c/p>\n<ul>\n<li><code>filename\x3c/code>: name of csv file\x3c/li>\n<li><code>delimiter\x3c/code>: Default: <span class=\"name\">,\x3c/span>\x3c/li>\n<li><code>line_numbers\x3c/code>: List of linenumbers to read. Default None\x3c/li>\n<li><code>quoting\x3c/code> (int): <i>0\x3c/i>: QUOTE_MINIMAL <i>1\x3c/i>: QUOTE_ALL <i>2\x3c/i>: QUOTE_NONNUMERIC <i>3\x3c/i>: QUOTE_NONE\x3c/li>\n\x3c/ul>","matched":true,"name":"Read Csv File To List","shortdoc":"Read CSV file and return its content as a Python list of tuples.","tags":[]}],"name":"CSVLibrary","named_args":true,"scope":"test case","version":""};
551551
</script>
552552
<title></title>
553553
</head>

0 commit comments

Comments
 (0)