From 01a5b263e5a5c9de2aedbedc9065dca0b4b0a9d2 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Sat, 14 Nov 2020 17:49:36 +0000 Subject: [PATCH] encoding/csv: improve Reader docs (#6828) --- vlib/encoding/csv/reader.v | 4 +++- vlib/encoding/csv/readme.md | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 vlib/encoding/csv/readme.md diff --git a/vlib/encoding/csv/reader.v b/vlib/encoding/csv/reader.v index e37d2f4139..ecd5475725 100644 --- a/vlib/encoding/csv/reader.v +++ b/vlib/encoding/csv/reader.v @@ -28,6 +28,7 @@ pub mut: row_pos int } +// new_reader initializes a Reader with string data to parse pub fn new_reader(data string) &Reader { return &Reader{ delimiter: `,`, @@ -36,7 +37,8 @@ pub fn new_reader(data string) &Reader { } } -// read() reads one row from the csv file +// read reads a row from the CSV data. +// If successful, the result holds an array of each column's data. pub fn (mut r Reader) read() ?[]string { l := r.read_record()? return l diff --git a/vlib/encoding/csv/readme.md b/vlib/encoding/csv/readme.md new file mode 100644 index 0000000000..e71a8650b5 --- /dev/null +++ b/vlib/encoding/csv/readme.md @@ -0,0 +1,19 @@ +## Reader example + +```v +import encoding.csv + +data := 'x,y\na,b,c\n' +mut parser := csv.new_reader(data) + +// read each line +for { + items := parser.read() or {break} + println(items) +} +``` +It prints: +``` +['x', 'y'] +['a', 'b', 'c'] +```