BurntSushi / BurntSushi/rust-csv

Ability to always quote String fields

Open
#165 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
2k
Forks
257
PR merge metrics
No merged PRs in 30d

Description

#### What version of the `csv` crate are you using?

1.1.1

#### Briefly describe the question, bug or feature request.

For writing csv files, I can't figure out how to have it always quote string fields but leave numeric fields alone. Right now if I switch the QuoteStyle to NonNumeric, it will still try and parse string fields as numbers and leave the quotes out if its able to parse it as a number. I'm not sure if this is possible in some other way, but I couldn't figure it out

#### Include a complete program demonstrating a problem.

I'd like to following code to have quotes around all the values in the `abc` column. I have no idea how difficult it would be to add, but I assume adding a new QuoteStyle would be the way to do it.

[Playground Link](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=959accf9e73002efed16c3866aba5fb7)

```
use csv::{QuoteStyle, WriterBuilder}; // 1.1.1
use serde; // 1.0.97
use std::io;

#[derive(serde::Serialize)]
struct Dummy {
abc: String,
def: f64,
}

fn main() {
let mut wrt = WriterBuilder::new()
.quote_style(QuoteStyle::NonNumeric)
.from_writer(io::stdout());
wrt.serialize(Dummy {
abc: "1.01".to_string(),
def: 1.01,
}).unwrap();
wrt.serialize(Dummy {
abc: "1.01.01".to_string(),
def: 1.01,
}).unwrap();
}
```

#### What is the observed behavior of the code above?

```
"abc","def"
1.01,1.01
"1.01.01",1.01
```

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

```
"abc","def"
"1.01",1.01
"1.01.01",1.01
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.