dummy-link

ReadWriteDlm2

readdlm2() and writedlm2(): Support decimal comma, semicolon delimiter and Date/Time types

Readme

ReadWriteDlm2 - Read And Write Decimal Comma "CSV"

ReadWriteDlm2 ReadWriteDlm2 Build Status Build status codecov.io

The functions readdlm2() and writedlm2() of module ReadWriteDlm2 are similar to readdlm() and writedlm() of Julia Base. Differences in usage are: ';' as default delimiter, ',' as default decimal mark and the support of Date/DateTime types. The basic idea of this package is to support the "decimal comma countries" - highlighted in green in the following map:

DecimalSeparator.svg
Map provided by NuclearVacuum - File:BlankMap-World6.svg , CC BY-SA 3.0, Link

Installation

This package is registered and can be installed with:

Pkg.add("ReadWriteDlm2")

Basic Example: How To Use ReadWriteDlm2

julia> using ReadWriteDlm2              # make readdlm2() and writedlm2() available

julia> A = [1 1.2; "text" Date(2017)];  # create test array with: Int, Float64, String and Date type

julia> writedlm2("test.csv", A)         # syntax and arguments like Julia.Base writedlm()

julia> readstring("test.csv")           # show `CSV` file. Please note: decimal mark ',' - delimiter ';'
"1;1,2\ntext;2017-01-01\n"

julia> B = readdlm2("test.csv")         # read `CSV` data: All four types are parsed correctly!
2×2 Array{Any,2}:
 1        1.2
  "text"   2017-01-01

Function readdlm2()

Read a matrix from source. The source can be a text file, stream or byte array. Each line, separated by eol (default is '\n'), gives one row. The columns are separated by ';', another delim can be defined.

readdlm2(source; options...)
readdlm2(source, T::Type; options...)
readdlm2(source, delim::Char; options...)
readdlm2(source, delim::Char, T::Type; options...)
readdlm2(source, delim::Char, eol::Char; options...)
readdlm2(source, delim::Char, T::Type, eol::Char; options...)

Pre-processing of source with regex substitution changes the decimal marks from d,d to d.d. For default rs the keyword argument decimal=',' sets the decimal Char in the r-string of rs. When a special regex substitution tuple rs=(r.., s..) is defined, the argument decimal is not used. Pre-processing can be switched off with: rs=().

In addition to Base readdlm(), strings are also parsed for ISO Date and DateTime formats by default. To switch off parsing Dates formats set: dfs="", dtfs="".

If all data is numeric, the result will be a numeric array. In other cases a heterogeneous array of numbers, dates and strings is returned.

Documentation For Base readdlm()

More information about Base functionality and (keyword) arguments - which are also supported by readdlm2() - is available in the stable documentation for readdlm().

Additional Keyword Arguments readdlm2()

  • decimal=',': decimal mark Char used by default rs, irrelevant if rs-tuple is not the default one
  • rs=(r"(\d),(\d)", s"."): regular expression (r, s)-tuple, change d,d to d.d if decimal=','
  • dfs="yyyy-mm-dd": format string for Date parsing, default is ISO
  • dtfs="yyyy-mm-ddTHH:MM:SS": format string for DateTime parsing, default is ISO

Compare Default Functionality readdlm() - readdlm2()

Module Function Delimiter Dec.Mark Date(Time)
Base.DataFmt readdlm() ' ' '.' n.a.(String)
ReadWriteDlm2 readdlm2() ';' ',' parse ISO

readdlm2(source, ' ', decimal='.', dfs="", dtfs="") gives the same result as readdlm(source).

Example readdlm2()

Read the Excel (lang=german) text-file test_de.csv and store the array in data:

data = readdlm2("test_de.csv", dfs="dd.mm.yyyy", dtfs="dd.mm.yyyy HH:MM")

Function writedlm2()

Write A (a vector, matrix, or an iterable collection of iterable rows) as text to f (either a filename string or an IO stream). The columns will be separated by ';', another delim (Char or String) can be defined.

writedlm2(f::IO, A; options...)
writedlm2(f::IO, A, delim; options...)
writedlm2(f::AbstractString, A; options...)
writedlm2(f::AbstractString, A, delim; options...)

By default, a pre-processing of floats takes place. Floats are parsed to strings with decimal mark changed from '.' to ','. With a keyword argument another decimal mark can be defined. To switch off this pre-processing set: decimal='.'.

Like writedlm() of Base, writedlm2() writes 3000.0 by default short as 3e3. When reading a file with mixed data, 3e3 will be parsed as Integer. To write long like print() set: write_short=false.

In writedlm2() the output format for Date and DateTime data can be defined with format strings. Defaults are the ISO formats.

Documentation For Base writedlm()

More information about Base functionality and (keyword-) arguments - which are also supported by writedlm2() - is available in the stable documentation for writedlm().

Additional Keyword Arguments writedlm2()

  • decimal=',': decimal mark character, default is a comma
  • write_short=true: Bool - use print_shortest() to write data, set false for print()
  • dfs="yyyy-mm-dd": format string, Date write format, default is ISO
  • dtfs="yyyy-mm-ddTHH:MM:SS": format string, DateTime write format, default is ISO

Compare Default Functionality writedlm() - writedlm2()

Module Function Delimiter Dec.Mark Date(Time)
Base.DataFmt writedlm() '\t' '.' ISO-Format
ReadWriteDlm2 writedlm2() ';' ',' ISO-Format

writedlm2(f, A, '\t', decimal='.') gives the same result as writedlm(f, A).

Example writedlm2()

Write Julia data to text-file test_de.csv, readable by Excel (lang=german):

writedlm2("test_de.csv", data, dtfs="dd.mm.yyyy HH:MM", dfs="dd.mm.yyyy")
julia-observer-html-cut-paste-4__work