Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-io/src/csv/write/options.rs
6939 views
1
use std::num::NonZeroUsize;
2
3
#[cfg(feature = "serde")]
4
use serde::{Deserialize, Serialize};
5
6
/// Options for writing CSV files.
7
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
8
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9
#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
10
pub struct CsvWriterOptions {
11
pub include_bom: bool,
12
pub include_header: bool,
13
pub batch_size: NonZeroUsize,
14
pub serialize_options: SerializeOptions,
15
}
16
17
impl Default for CsvWriterOptions {
18
fn default() -> Self {
19
Self {
20
include_bom: false,
21
include_header: true,
22
batch_size: NonZeroUsize::new(1024).unwrap(),
23
serialize_options: SerializeOptions::default(),
24
}
25
}
26
}
27
28
/// Options to serialize logical types to CSV.
29
///
30
/// The default is to format times and dates as `chrono` crate formats them.
31
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
32
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
33
#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
34
pub struct SerializeOptions {
35
/// Used for [`DataType::Date`](polars_core::datatypes::DataType::Date).
36
pub date_format: Option<String>,
37
/// Used for [`DataType::Time`](polars_core::datatypes::DataType::Time).
38
pub time_format: Option<String>,
39
/// Used for [`DataType::Datetime`](polars_core::datatypes::DataType::Datetime).
40
pub datetime_format: Option<String>,
41
/// Used for [`DataType::Float64`](polars_core::datatypes::DataType::Float64)
42
/// and [`DataType::Float32`](polars_core::datatypes::DataType::Float32).
43
pub float_scientific: Option<bool>,
44
pub float_precision: Option<usize>,
45
/// Use comma as the decimal separator.
46
pub decimal_comma: bool,
47
/// Used as separator.
48
pub separator: u8,
49
/// Quoting character.
50
pub quote_char: u8,
51
/// Null value representation.
52
pub null: String,
53
/// String appended after every row.
54
pub line_terminator: String,
55
/// When to insert quotes.
56
pub quote_style: QuoteStyle,
57
}
58
59
impl Default for SerializeOptions {
60
fn default() -> Self {
61
Self {
62
date_format: None,
63
time_format: None,
64
datetime_format: None,
65
float_scientific: None,
66
float_precision: None,
67
decimal_comma: false,
68
separator: b',',
69
quote_char: b'"',
70
null: String::new(),
71
line_terminator: "\n".into(),
72
quote_style: Default::default(),
73
}
74
}
75
}
76
77
/// Quote style indicating when to insert quotes around a field.
78
#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq)]
79
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
80
#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
81
pub enum QuoteStyle {
82
/// Quote fields only when necessary.
83
///
84
/// Quotes are necessary when fields contain a quote, separator or record terminator.
85
/// Quotes are also necessary when writing an empty record (which is indistinguishable
86
/// from arecord with one empty field).
87
/// This is the default.
88
#[default]
89
Necessary,
90
/// Quote every field. Always.
91
Always,
92
/// Quote non-numeric fields.
93
///
94
/// When writing a field that does not parse as a valid float or integer,
95
/// quotes will be used even if they aren't strictly necessary.
96
NonNumeric,
97
/// Never quote any fields, even if it would produce invalid CSV data.
98
Never,
99
}
100
101