Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected UTF-8 error #268

Open
RedDocMD opened this issue Feb 24, 2022 · 0 comments
Open

Unexpected UTF-8 error #268

RedDocMD opened this issue Feb 24, 2022 · 0 comments

Comments

@RedDocMD
Copy link

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

use std::path::Path;

use csv::{ReaderBuilder, Terminator};
use serde::{de::DeserializeOwned, Deserialize};

fn main() {
    let filename = std::env::args().next().unwrap();
    let records: Vec<PopulationRecord> = read_records(filename).unwrap();
    println!("{:?}", records);
}

#[derive(Debug, Deserialize)]
struct PopulationRecord {
    city: String,
    state: String,
    country: String,
    population: u64,
}

fn read_records<P: AsRef<Path>, R: DeserializeOwned>(path: P) -> Result<Vec<R>, csv::Error> {
    let mut rdr = ReaderBuilder::new()
        .has_headers(false)
        .terminator(Terminator::CRLF)
        .from_path(path)?;
    rdr.deserialize().collect()
}

Observed behaviour

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

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error(Utf8 { pos: Some(Position { byte: 0, line: 1, record: 0 }), err: Utf8Error { field: 0, valid_up_to: 25 } })', src/main.rs:8:65
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Expected behaviour

Should give no error, but print the records.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant