encoding/csv: improve Reader docs (#6828)

pull/6832/head
Nick Treleaven 2020-11-14 17:49:36 +00:00 committed by GitHub
parent 00464ad988
commit 01a5b263e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -28,6 +28,7 @@ pub mut:
row_pos int row_pos int
} }
// new_reader initializes a Reader with string data to parse
pub fn new_reader(data string) &Reader { pub fn new_reader(data string) &Reader {
return &Reader{ return &Reader{
delimiter: `,`, 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 { pub fn (mut r Reader) read() ?[]string {
l := r.read_record()? l := r.read_record()?
return l return l

View File

@ -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']
```