1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
defmodule CSV do
use CSV.Defaults
alias CSV.Decoding.Decoder
alias CSV.Encoding.Encoder
@moduledoc ~S"""
RFC 4180 compliant CSV parsing and encoding for Elixir. Allows to specify
other separators, so it could also be named: TSV, but it isn't.
"""
@doc """
Decode a stream of comma-separated lines into a stream of tuples. Decoding
errors will be inlined into the stream.
## Options
These are the options:
* `:separator` – The separator token to use, defaults to `?,`.
Must be a codepoint (syntax: ? + (your separator)).
* `:escape_character` – The escape character token to use, defaults to `?"`.
Must be a codepoint (syntax: ? + (your escape character)).
* `:escape_max_lines` – The number of lines an escape sequence is allowed
to span, defaults to 10.
* `:field_transform` – A function with arity 1 that will get called with
each field and can apply transformations. Defaults to identity function.
This function will get called for every field and therefore should return
quickly.
* `:headers` – When set to `true`, will take the first row of
the csv and use it as header values.
When set to a list, will use the given list as header values.
When set to `false` (default), will use no header values.
When set to anything but `false`, the resulting rows in the matrix will
be maps instead of lists.
* `:validate_row_length` – When set to `true`, will take the first row of
the csv or its headers and validate that following rows are of the same
length. Defaults to `false`.
* `:unescape_formulas` – When set to `true`, will remove formula escaping
inserted to prevent [CSV Injection](https://owasp.org/www-community/attacks/CSV_Injection).
* `:redact_errors` – When set to `true`, will redact csv data in
message output of errors. This is to address scenarios where errors are
propagated in a stream and accidental logging of sensitive data needs
to be prevented. Defaults to `false`.
## Examples
Convert a filestream into a stream of rows in order of the given stream:
iex> \"../test/fixtures/docs/valid.csv\"
iex> |> Path.expand(__DIR__)
iex> |> File.stream!
iex> |> CSV.decode
iex> |> Enum.take(2)
[ok: [\"a\",\"b\",\"c\"], ok: [\"d\",\"e\",\"f\"]]
Read from a file with a Byte Order Mark (BOM):
iex> \"../test/fixtures/utf8-with-bom.csv\"
...> |> Path.expand(__DIR__)
...> |> File.stream!([:trim_bom])
...> |> CSV.decode()
...> |> Enum.take(2)
[ok: [\"a\", \"b\"], ok: [\"d\", \"e\"]]
Read from an UTF-16 encoded file with a Byte Order Mark (BOM):
iex> \"../test/fixtures/utf16-little-with-bom.csv\"
...> |> Path.expand(__DIR__)
...> |> File.stream!([:trim_bom, encoding: {:utf16, :little}])
...> |> CSV.decode()
...> |> Enum.take(3)
[ok: [\"a\", \"b\", \"c\"], ok: [\"1\", \"2\", \"3\"], ok: [\"4\", \"5\", \"ʤ\"]]
Read from an UTF-16 big endian encoded file with a Byte Order Mark (BOM):
iex> \"../test/fixtures/utf16-big-with-bom.csv\"
...> |> Path.expand(__DIR__)
...> |> File.stream!([:trim_bom, encoding: {:utf16, :big}])
...> |> CSV.decode()
...> |> Enum.take(3)
[ok: [\"a\", \"b\", \"c\"], ok: [\"1\", \"2\", \"3\"], ok: [\"4\", \"5\", \"ʤ\"]]
Errors will show up as error tuples:
iex> \"../test/fixtures/docs/escape-errors.csv\"
iex> |> Path.expand(__DIR__)
iex> |> File.stream!
iex> |> CSV.decode
iex> |> Enum.take(2)
[
ok: [\"a\",\"b\",\"c\"],
error: "Escape sequence started on line 2:\\n\\n\\"d,e,f\\n\\n\
did not terminate before the stream halted. Parsing will continue on line 3.\\n"
]
Map an existing stream of lines separated by a token to a stream of rows
with a header row:
iex> [\"a;b\\n\",\"c;d\\n\", \"e;f\\n\"]
iex> |> Stream.map(&(&1))
iex> |> CSV.decode(separator: ?;, headers: true)
iex> |> Enum.take(2)
[
ok: %{\"a\" => \"c\", \"b\" => \"d\"},
ok: %{\"a\" => \"e\", \"b\" => \"f\"}
]
Map a stream with custom escape characters:
iex> [\"@a@,@b@\\n\",\"@c@,@d@\\n\"]
...> |> Stream.map(&(&1))
...> |> CSV.decode(escape_character: ?@)
...> |> Enum.take(2)
[ok: [\"a\", \"b\"], ok: [\"c\", \"d\"]]
Map a stream with custom separator characters:
iex> [\"a;b\\n\",\"c;d\\n\"]
...> |> Stream.map(&(&1))
...> |> CSV.decode(separator: ?;)
...> |> Enum.take(2)
[ok: [\"a\", \"b\"], ok: [\"c\", \"d\"]]
Trim each field:
iex> [\" a , b \\n\",\" c , d \\n\"]
...> |> Stream.map(&(&1))
...> |> CSV.decode(field_transform: &String.trim/1)
...> |> Enum.take(2)
[ok: [\"a\", \"b\"], ok: [\"c\", \"d\"]]
Map an existing stream of lines separated by a token to a stream of rows
with a given header row:
iex> [\"a;b\\n\",\"c;d\\n\", \"e;f\\n\"]
iex> |> Stream.map(&(&1))
iex> |> CSV.decode(separator: ?;, headers: [:x, :y])
iex> |> Enum.take(2)
[
ok: %{:x => \"a\", :y => \"b\"},
ok: %{:x => \"c\", :y => \"d\"}
]
"""
@type decode_options ::
{:separator, char}
| {:field_transform, (String.t() -> String.t())}
| {:headers, [String.t() | atom()] | boolean()}
| {:unescape_formulas, boolean()}
| {:validate_row_length, boolean()}
| {:escape_character, char()}
| {:escape_max_lines, integer()}
| ({:redact_errors, boolean()}
| {:unredact_exceptions, boolean()})
@spec decode(Enumerable.t(), [decode_options()]) :: Enumerable.t()
def decode(stream, options \\ []) do
stream |> Decoder.decode(options) |> inline_errors!(options)
end
@doc """
Decode a stream of comma-separated lines into a stream of tuples. Errors
when decoding will get raised immediately.
## Options
These are the options:
* `:separator` – The separator token to use, defaults to `?,`.
Must be a codepoint (syntax: ? + (your separator)).
* `:escape_character` – The escape character token to use, defaults to `?"`.
Must be a codepoint (syntax: ? + (your escape character)).
* `:field_transform` – A function with arity 1 that will get called with
each field and can apply transformations. Defaults to identity function.
This function will get called for every field and therefore should return
quickly.
* `:headers` – When set to `true`, will take the first row of
the csv and use it as header values.
When set to a list, will use the given list as header values.
When set to `false` (default), will use no header values.
When set to anything but `false`, the resulting rows in the matrix will
be maps instead of lists.
* `:validate_row_length` – When set to `true`, will take the first row of
the csv or its headers and validate that following rows are of the same
length. Will raise an error if validation fails. Defaults to `false`.
* `:unescape_formulas` – When set to `true`, will remove formula escaping
inserted to prevent [CSV Injection](https://owasp.org/www-community/attacks/CSV_Injection).
* `:unredact_exceptions` – When set to `true`, will show csv data in
message output of exceptions thrown. Only use this when using CSV strict
mode in environments and situations where there is no concern with
exception message data leaking in logs. Defaults to `false`.
## Examples
Convert a filestream into a stream of rows in order of the given stream:
iex> \"../test/fixtures/docs/valid.csv\"
iex> |> Path.expand(__DIR__)
iex> |> File.stream!()
iex> |> CSV.decode!()
iex> |> Enum.take(2)
[[\"a\",\"b\",\"c\"], [\"d\",\"e\",\"f\"]]
Read from a file with a Byte Order Mark (BOM):
iex> \"../test/fixtures/utf8-with-bom.csv\"
...> |> Path.expand(__DIR__)
...> |> File.stream!([:trim_bom])
...> |> CSV.decode!()
...> |> Enum.take(2)
[[\"a\", \"b\"], [\"d\", \"e\"]]
Read from an UTF-16 encoded file with a Byte Order Mark (BOM):
iex> \"../test/fixtures/utf16-little-with-bom.csv\"
...> |> Path.expand(__DIR__)
...> |> File.stream!([:trim_bom, encoding: {:utf16, :little}])
...> |> CSV.decode!()
...> |> Enum.take(3)
[[\"a\", \"b\", \"c\"], [\"1\", \"2\", \"3\"], [\"4\", \"5\", \"ʤ\"]]
Read from an UTF-16 big endian encoded file with a Byte Order Mark (BOM):
iex> \"../test/fixtures/utf16-big-with-bom.csv\"
...> |> Path.expand(__DIR__)
...> |> File.stream!([:trim_bom, encoding: {:utf16, :big}])
...> |> CSV.decode!()
...> |> Enum.take(3)
[[\"a\", \"b\", \"c\"], [\"1\", \"2\", \"3\"], [\"4\", \"5\", \"ʤ\"]]
Map an existing stream of lines separated by a token to a stream of rows
with a header row:
iex> [\"a;b\\n\",\"c;d\\n\", \"e;f\"]
iex> |> Stream.map(&(&1))
iex> |> CSV.decode!(separator: ?;, headers: true)
iex> |> Enum.take(2)
[
%{\"a\" => \"c\", \"b\" => \"d\"},
%{\"a\" => \"e\", \"b\" => \"f\"}
]
Map a stream with custom escape characters:
iex> [\"@a@,@b@\\n\",\"@c@,@d@\\n\"]
...> |> Stream.map(&(&1))
...> |> CSV.decode!(escape_character: ?@)
...> |> Enum.take(2)
[[\"a\", \"b\"], [\"c\", \"d\"]]
Map an existing stream of lines separated by a token to a stream of rows
with a given header row:
iex> [\"a;b\\n\",\"c;d\\n\", \"e;f\"]
iex> |> Stream.map(&(&1))
iex> |> CSV.decode!(separator: ?;, headers: [:x, :y])
iex> |> Enum.take(2)
[
%{:x => \"a\", :y => \"b\"},
%{:x => \"c\", :y => \"d\"}
]
Trim each field:
iex> [\" a , b \\n\",\" c , d \\n\"]
...> |> Stream.map(&(&1))
...> |> CSV.decode!(field_transform: &String.trim/1)
...> |> Enum.take(2)
[[\"a\", \"b\"], [\"c\", \"d\"]]
Replace invalid codepoints:
iex> "../test/fixtures/broken-encoding.csv"
...> |> Path.expand(__DIR__)
...> |> File.stream!()
...> |> CSV.decode!(field_transform: fn field ->
...> if String.valid?(field) do
...> field
...> else
...> field
...> |> String.codepoints()
...> |> Enum.map(fn codepoint -> if String.valid?(codepoint), do: codepoint, else: "?" end)
...> |> Enum.join()
...> end
...> end)
...> |> Enum.take(2)
[["a", "b", "c", "?_?"], ["ಠ_ಠ"]]
"""
@spec decode!(Enumerable.t(), [decode_options()]) :: Enumerable.t()
def decode!(stream, options \\ []) do
stream |> Decoder.decode(options) |> raise_errors!(options)
end
defp raise_errors!(stream, options) do
escape_max_lines = options |> Keyword.get(:escape_max_lines, @escape_max_lines)
unredact_exceptions = options |> Keyword.get(:unredact_exceptions, false)
stream |> Stream.map(&yield_or_raise!(&1, escape_max_lines, unredact_exceptions))
end
defp yield_or_raise!({:error, mod, args}, _, unredact_exceptions) do
raise mod, args ++ [mode: :strict, unredact: unredact_exceptions]
end
defp yield_or_raise!({:ok, row}, _, _), do: row
defp inline_errors!(stream, options) do
escape_max_lines = options |> Keyword.get(:escape_max_lines, @escape_max_lines)
redact_errors = options |> Keyword.get(:redact_errors, false)
stream |> Stream.map(&yield_or_inline!(&1, escape_max_lines, redact_errors))
end
defp yield_or_inline!({:error, mod, args}, _, redact_errors) do
{:error, mod.exception(args ++ [mode: :normal, unredact: !redact_errors]).message}
end
defp yield_or_inline!(value, _, _), do: value
@doc """
Encode a table stream into a stream of RFC 4180 compliant CSV lines for
writing to a file or other IO.
## Options
These are the options:
* `:separator` – The separator token to use, defaults to `?,`.
Must be a codepoint (syntax: ? + (your separator)).
* `:escape_character` – The escape character token to use, defaults to `?"`.
Must be a codepoint (syntax: ? + (your escape character)).
* `:delimiter` – The delimiter token to use, defaults to `\\r\\n`.
Must be a string.
* `:force_escaping` – When set to `true`, will escape fields even if
they do not contain characters that require escaping
* `:escape_formulas` – When set to `true`, will escape formulas
to prevent [CSV Injection](https://owasp.org/www-community/attacks/CSV_Injection).
* `:headers`
* When set to `false` (default), will use the raw inputs as elements. When set to anything but `false`, all elements in the input stream are assumed to be maps.
* When set to `true`, uses the keys of the first map as the first
element in the stream. All subsequent elements are the values of the maps.
* When set to a list, will use the given list as the first element
in the stream and order all subsequent elements using that list.
* When set to a keyword list, will use the keys of the
keyword list to match the keys of the data, and the values of the
keyword list to be the values in the first row of the output.
## Examples
Convert a stream of rows with fields into a stream of lines:
iex> [~w(a b), ~w(c d)]
iex> |> CSV.encode
iex> |> Enum.take(2)
[\"a,b\\r\\n\", \"c,d\\r\\n\"]
Convert a stream of rows with fields with escape sequences into a stream of
lines:
iex> [[\"a\\nb\", \"\\tc\"], [\"de\", \"\\tf\\\"\"]]
iex> |> CSV.encode(separator: ?\\t, delimiter: \"\\n\")
iex> |> Enum.take(2)
[\"\\\"a\\nb\\\"\\t\\\"\\tc\\\"\\n\", \"de\\t\\\"\\tf\\\"\\\"\\\"\\n\"]
Convert a stream of rows with fields into a stream of lines forcing escaping
with a custom character:
iex> [~w(a b), ~w(c d)]
iex> |> CSV.encode(force_escaping: true, escape_character: ?@)
iex> |> Enum.take(2)
[\"@a@,@b@\\r\\n\", \"@c@,@d@\\r\\n\"]
Convert a stream of rows with fields with formulas into a stream of
lines:
iex> [~w(@a =b), ~w(-c +d)]
iex> |> CSV.encode(escape_formulas: true)
iex> |> Enum.take(2)
[\"\\\"'@a\\\",\\\"'=b\\\"\\r\\n\", \"\\\"'-c\\\",\\\"'+d\\\"\\r\\n\"]
Convert a stream of row maps
iex> [%{header1: "header1 value1", header2: "header2 value1"}]
...> |> CSV.encode(headers: true)
...> |> Enum.to_list()
["header1,header2\\r\\n", "header1 value1,header2 value1\\r\\n"]
Convert a stream of rows renaming the headers by passing in a keyword list
iex> [%{a: "value!"}]
...> |> CSV.encode(headers: [a: "x", b: "y"])
...> |> Enum.to_list()
["x,y\\r\\n", "value!,\\r\\n"]
"""
@type encode_options ::
{:separator, char()}
| {:escape_character, char()}
| {:headers, [String.t() | atom()] | Keyword.t() | boolean()}
| {:delimiter, String.t()}
| {:force_escaping, boolean()}
| {:escape_formulas, boolean()}
@spec encode(Enumerable.t(), [encode_options()]) :: Enumerable.t()
def encode(stream, options \\ []) do
Encoder.encode(stream, options)
end
end
|