how do I write column oriented data to a CSV format? #340
-
Hello. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
CSV data is written one row at a time. That's a fundamental fact about the CSV format and there is no way to change it. Thus, if your data is arranged by column and you need to format it as CSV data, then the only choice available to you is to convert your data such that it is arranged by row in some way. Here's one somewhat simplistic example that will write a use std::collections::HashMap;
fn main() -> anyhow::Result<()> {
let mut data = HashMap::new();
data.insert("field1".to_string(), vec![1, 2, 3, 4, 5]);
data.insert("field2".to_string(), vec![10, 20, 30, 40, 50]);
data.insert("field3".to_string(), vec![100, 200, 300, 400, 500]);
write_column_data(&data)?;
Ok(())
}
fn write_column_data(
columns: &HashMap<String, Vec<i64>>,
) -> anyhow::Result<()> {
let mut fields: Vec<&str> = columns.keys().map(|k| k.as_str()).collect();
fields.sort();
let mut iters: Vec<std::slice::Iter<'_, i64>> =
fields.iter().map(|f| columns[*f].iter()).collect();
let mut wtr = csv::Writer::from_writer(std::io::stdout().lock());
wtr.write_record(fields.iter())?;
'OUTER: loop {
for it in iters.iter_mut() {
let Some(value) = it.next() else { break 'OUTER };
wtr.write_field(value.to_string())?;
}
wtr.write_record(None::<&[u8]>)?;
}
wtr.flush()?;
Ok(())
} And the [package]
publish = false
name = "d340"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.75"
csv = "1.3.0"
[[bin]]
name = "d340"
path = "main.rs" And running the program and its output:
Note that this program assumes every column has the same length. If that isn't true the code above might not work as you expect it will. |
Beta Was this translation helpful? Give feedback.
CSV data is written one row at a time. That's a fundamental fact about the CSV format and there is no way to change it. Thus, if your data is arranged by column and you need to format it as CSV data, then the only choice available to you is to convert your data such that it is arranged by row in some way.
Here's one somewhat simplistic example that will write a
HashMap<String, Vec<i64>>
as CSV data: