What is the canonical way to read CSV in Zig?

Hi,
I’m newbie coming from Python & Rust, trying to figure out what’s available in the language. I googled and searched the forum, but only see links to a couple of libs, or better say repos.

I needed to read CSV file and extract some data. In Rust, I’d make a struct and use serde crate to iterate the lines as entities of this type.

The Github repos (beho/zig-csv and DISTREAT/zig-csv) with their exapmles look very-very basic.

Is there a way to work with CSV similar to the Rust way (at least, I need only int/float/string types), or it’s too early to ask for it?

std.mem has some stuff for that to get you started. Look for split, tokenize etc.

3 Likes
2 Likes

Thanks, that’s a lot better than I expected!

Once you have read a line, here is some generic code to populate a struct

2 Likes

Some implementations of CSV allow for line breaks within a value (Excel, for example). So for some applications you, can’t process CSV on a line-by line-basis.

1 Like

Never combine excel with multiline csv. Real Life Experience.

CSV indeed can contain multiline fields. Needs special care and parsing.

1 Like