burntsushi/rust-csv

A fast and flexible CSV reader and writer for Rust, with support for Serde

Dual-licensed under MIT or the If you're new to Rust, the

csv

A fast and flexible CSV reader and writer for Rust, with support for Serde.

Dual-licensed under MIT or the UNLICENSE.

Documentation

https://docs.rs/csv

If you're new to Rust, the tutorial is a good place to start.

Usage

Add this to your Cargo.toml:

Example

This example shows how to read CSV data from stdin and print each record to stdout.

There are more examples in the cookbook.

The above example can be run like so:

Example with Serde

This example shows how to read CSV data from stdin into your own custom struct. By default, the member names of the struct are matched with the values in the header record of your CSV data.

The above example can be run like so:

Issues

Collection of the latest Issues

yyzdtccjdtc

yyzdtccjdtc

enhancement
Comment Icon1

https://github.com/BurntSushi/rust-csv/blob/41c71ed353a71526c52633d854466c1619dacae4/csv-core/src/reader.rs#L669

For this line of code, it will always update the value of self.line, even if the value of it does not change. This introduces some redundant operations.

If we change the code to this, we can eliminate these redundant operations.

According to my tests with the ema example with this application (https://github.com/greyblake/ta-rs), the average execution time decreased from 2.74s to 2.68s, which is a 1.02x speedup.

Hope this information helps!

nlordell

nlordell

Comment Icon2

Version

Tested on both v1.1.6 and master branch.

Briefly describe the bug.

Deserializing a struct with a flattened tagged enum field does not seem to work.

Include a complete program demonstrating a problem.

Code:

(Playground link).

What is the observed behavior of the code above?

Fails to deserialize with invalid type: byte array, expected "kind", "parameter", or other ignored fields error:

What is the expected or desired behavior of the code above?

To correctly deserialize records as they are done in the case of flattened structs (also included in code example).

hniksic

hniksic

bug
Comment Icon4

The docs for QuoteStyle::Never say:

This never writes quotes, even if it would produce invalid CSV data.

Based on that, I would expect the following program to output a line with foo followed by an empty line, followed by a line with bar,baz:

Expected output:

But actual output is:

Is there a way to prevent the writer from generating quotes?

I am using csv 1.1.6.

aleb

aleb

doc
Comment Icon14

What version of the csv crate are you using?

csv = "1.1.6"

Briefly describe the question, bug or feature request.

The header line is parsed as the rest of the lines, when it should not be parsed. This results in failure when the column names are parsed and validated with serde.

Include a complete program demonstrating a problem.

I need to use the following workaround to make it work:

This is the CSV:

Does not matter that has_headers is set to true or false, seems broken.

Also, note if I'm not manually skipping the first line, "name" is also parsed despite using set_headers() and despite has_headers(true), seems a different issue?

ColinFinck

ColinFinck

Comment Icon5

41c71ed353a71526c52633d854466c1619dacae4 brought us itoa 1.x, but it's not yet available in an official csv release. Should be fairly straightforward to release current master as 1.1.7

CC @BurntSushi @waywardmonkeys

RedDocMD

RedDocMD

Comment Icon0

version: 1.1.6

Problem

When deserializing a CSV file without headers into a struct (with serde), a UTF-8 error is returned, even though I think the file is valid UTF-8.

Code

Observed behaviour

I have downloaded the smallpop-no-headers.csv file from the examples directory. Then running the above program on it gives:

Expected behaviour

Should give no error, but print the records.

lukaslueg

lukaslueg

enhancement
Comment Icon5

I have a consumer that is generic over its input and expects an iterator of iterators of AsRef<str> items. Using csv 1.1.6, I could map the StringRecordsIter and it's StringRecord such as

This won't work as StringRecord only implements iteration over a borrowed StringRecord. Since sr goes away in the outer closure, the .map(|f: &str| ...) doesn't pass the borrow checker.

A workaround is to completely manually iterate over the fields, such as

... but this seems cumbersome. Am I missing something obvious here, or is a StringRecordIntoIterator akin to StringRecordsIntoIterator simply missing?

sqwishy

sqwishy

Comment Icon1

What I'm trying to do is deserialize a StringRecord but ignore some columns. The columns aren't known at compile time.

It's easy to find their positions and grab the cells with StringRecord::get but this doesn't make use of the deserializer so I have to infer/guess the type myself. Which really isn't all that bad really, but it might be favourable to use the serde infrastructure.

In my scenario, the columns I'm interested in deserializing is only known at run time. I can't figure out how to get this to work in the current API this crate provides. Here's the two things I've tried.

  1. I could use DeserializeSeed to deserialize a row using a stateful object that knows what what cells to skip over and which to deserialize. However, this takes a Deserializer and this library doesn't appear to offer that in the API.

  2. Since I know what cells I need, I could also use a form of StringRecord::get that also takes a type parameter that implements Deserialize and deserializes the cell to that type. Sort of like StringRecord::deserialize but for just the cell instead of the entire row.

Is there any way to accomplish what I'm trying to do using the current API that I've missed?

corvusrabus

corvusrabus

Comment Icon2

Hi, thanks for providing this great crate. Currently, when I am using the crate to serialise a vector inside a struct it will be flattened, so say I have:

struct A { a : String, b : Vec<i64> }

and I have a flexible writer, then two As {"Hello",[1,2,3]}, {"Bye", [4,5,6,7]} would be serialized to "Hello", 1,2,3, "Bye", 4,5,6,7

Is there any way I can tell the CSV writer to serialize it to "Hello", [1,2,3] "Bye", [4,5,6,7] ?

jrozner

jrozner

Comment Icon0

What version of the csv crate are you using?

1.1.6

Briefly describe the question, bug or feature request.

Serialize the header prefixed with a comment character (eg. '#')

Include a complete program demonstrating a problem.

What is the observed behavior of the code above?

name,type,subtype,offset,size,flags

What is the expected or desired behavior of the code above?

#name,type,subtype,offset,size,flags

multimeric

multimeric

doc
Comment Icon1

This is just a documentation suggestion so I'm not following the usual issue format if that's okay.

As a new user of this crate as of today, I noticed that reading through the main examples in the index and also the cookbook only interact with the records by using dbg!. This has the downside in that it doesn't show how to actually use a record in a real-life situation, which I believe is indexing it using .get() or [] to get at each of the "cells" in the CSV. I find the Serde example is quite clear because it's evident that you end up with an instance of Record, which has clearly defined types, it's only the non-Serde example where I wasn't sure what I was working with.

So my suggestion is to show this normal indexing behaviour in the main examples, and also link to the StringRecord examples for more detail.

I'm happy to write up a PR for this if you agree..

tristanCadet

tristanCadet

Comment Icon4

What version of the csv crate are you using?

version = "1.1.6"

Briefly describe the question, bug or feature request.

The program panics when serializing a struct that contains a nested struct decorated by #[serde(flatten)]. I am not sure whether this is a bug or expected behavior but I would expect the program to output a csv. If it is expected are there any alternatives to obtain the desired output (without removing nesting) ?

Include a complete program demonstrating a problem.

What is the observed behavior of the code above?

What is the expected or desired behavior of the code above?

Information - Updated Jun 30, 2022

Stars: 1.2K
Forks: 163
Issues: 69

Rust library for Self Organising Maps (SOM)

Add rusticsom as a dependency in Cargo

Rust library for Self Organising Maps (SOM)

Rust library for parsing configuration files

The 'option' can be any string with no whitespace

Rust library for parsing configuration files

Rust library for the Pimoroni Four Letter pHAT

This library aims to port ht16k33 (or rather a fork, as of right now) so credit goes to ht16k33-diet

Rust library for the Pimoroni Four Letter pHAT

Rust library for emulating 32-bit RISC-V

This library can execute instructions against any memory and register file that implements

Rust library for emulating 32-bit RISC-V

Rust library for connecting to the IPFS HTTP API using Hyper/Actix

You can use actix-web as a backend instead of hyper

Rust library for connecting to the IPFS HTTP API using Hyper/Actix

Rust library to manipulate file system access control lists (ACL) on macOS, Linux, and FreeBSD

This module provides two high level functions, getfacl and setfacl

Rust library to manipulate file system access control lists (ACL) on macOS, Linux, and FreeBSD

Rust library translation (rust-src/rust-std/stdlib/rustlib translation)

This is the place to translate Having a documentation in your native language is essential if you don't speak English, and still enjoyable even if...

Rust library translation (rust-src/rust-std/stdlib/rustlib translation)

Rust library for using Infrared hardware decoders (For example a Vishay TSOP* decoder),

enabling remote control support for embedded project

Rust library for using Infrared hardware decoders (For example a Vishay TSOP* decoder),

Rust library for interaction with the OriginTrail Decentralized Knowledge Graph

open up an issue on this repository and let us know

Rust library for interaction with the OriginTrail Decentralized Knowledge Graph

Rust library for parsing COLLADA files

Notice: This library is built around files exported from Blender 2

Rust library for parsing COLLADA files

Rust library for low-level abstraction of MIPS32 processors

This project is licensed under the terms of the MIT license

Rust library for low-level abstraction of MIPS32 processors
Facebook Instagram Twitter GitHub Dribbble
Privacy