Skip to content

Instantly share code, notes, and snippets.

@rccc
Created March 20, 2023 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rccc/8b71bbbc735794d6617a64517833df41 to your computer and use it in GitHub Desktop.
Save rccc/8b71bbbc735794d6617a64517833df41 to your computer and use it in GitHub Desktop.
import os
import encoding.csv
fn main(){
csv_name := 'test.csv'
content := os.read_file(csv_name) or {panic(err)}
mut reader := csv.new_reader(content)
reader.read()! // Skip the first line
for {
line := reader.read() or {break}
println(line)
}
}
@thomas-mangin
Copy link

not tested:

import os
import encoding.csv

fn read_csv(csv_name string)! {
        content := os.read_file(csv_name)!
        content.close()
        
        mut reader := csv.new_reader(content)

        reader.read()! // Skip the first line
        for {
            line := reader.read() or {break}
            println(line)
        }
}

fn main () {
        read_csv('test.csv') or { panic(err) }
}

@thomas-mangin
Copy link

Quickly looking at the code of the CVS module, the break should not be alone. There should be a check for the kind of error, and only break if it is "EndOfFileError", otherwise the error should be passed up. The test code does not do it, but it is not good practice - it may be ok for testing but should not be in prod. Have fun. Never used the code so good luck!

@rccc
Copy link
Author

rccc commented Apr 5, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment