Appends CSV records to a file.

By default, uses the format recommended by RFC 4180 (see rfc4180).

Note: If a record contains only an empty cell, its representation is undistinguishable from an empty line. This is because the empty values are always written unescaped in order to avoid them to be interpreted as escaped delimiters by some parsers.

var out = new StringWriter
var writer = new CsvWriter(out)
writer.write_elements(1, 2.0, "foo\nbar")
writer.write_line([""])
assert out.to_s == """1,2.0,"foo\nbar"\n\n"""

Introduced properties

init defaultinit(ostream: Writer)

csv :: CsvWriter :: defaultinit

fun ostream: Writer

csv :: CsvWriter :: ostream

The output stream.
protected fun ostream=(ostream: Writer)

csv :: CsvWriter :: ostream=

The output stream.
fun write_elements(els: Object...)

csv :: CsvWriter :: write_elements

Append the elements in els as a record.
fun write_line(line: Array[Object])

csv :: CsvWriter :: write_line

Append the specified record.
fun write_lines(lines: Array[Array[Object]])

csv :: CsvWriter :: write_lines

Write several lines to a stream

Redefined properties

redef type SELF: CsvWriter

csv $ CsvWriter :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
init defaultinit(ostream: Writer)

csv :: CsvWriter :: defaultinit

fun delimiter: Char

csv :: CsvStream :: delimiter

The character that delimits escaped value.
fun delimiter=(delimiter: Char)

csv :: CsvStream :: delimiter=

The character that delimits escaped value.
fun eol: String

csv :: CsvStream :: eol

The character that ends a record (end of line).
fun eol=(eol: String)

csv :: CsvStream :: eol=

The character that ends a record (end of line).
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun ostream: Writer

csv :: CsvWriter :: ostream

The output stream.
protected fun ostream=(ostream: Writer)

csv :: CsvWriter :: ostream=

The output stream.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun separator: Char

csv :: CsvStream :: separator

The character that split each cell in a record.
fun separator=(separator: Char)

csv :: CsvStream :: separator=

The character that split each cell in a record.
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun write_elements(els: Object...)

csv :: CsvWriter :: write_elements

Append the elements in els as a record.
fun write_line(line: Array[Object])

csv :: CsvWriter :: write_line

Append the specified record.
fun write_lines(lines: Array[Array[Object]])

csv :: CsvWriter :: write_lines

Write several lines to a stream
package_diagram csv::CsvWriter CsvWriter csv::CsvStream CsvStream csv::CsvWriter->csv::CsvStream core::Object Object csv::CsvStream->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

abstract class CsvStream

csv :: CsvStream

Shared properties by all CSV-related classes

Class definitions

csv $ CsvWriter
# Appends CSV records to a file.
#
# By default, uses the format recommended by RFC 4180 (see `rfc4180`).
#
# Note: If a record contains only an empty cell, its representation is
# undistinguishable from an empty line. This is because the empty values are
# always written unescaped in order to avoid them to be interpreted as escaped
# delimiters by some parsers.
#
# ~~~nit
# var out = new StringWriter
# var writer = new CsvWriter(out)
# writer.write_elements(1, 2.0, "foo\nbar")
# writer.write_line([""])
# assert out.to_s == """1,2.0,"foo\nbar"\n\n"""
# ~~~
class CsvWriter
	super CsvStream

	# The output stream.
	var ostream: Writer

	# Write several lines to a stream
	fun write_lines(lines: Array[Array[Object]]) do for i in lines do write_line i

	# Append the elements in `els` as a record.
	#
	# The representation of each cell is determined by `to_s`.
	fun write_elements(els: Object...) do
		var os = ostream
		var esc = delimiter
		var sep = separator
		var eol = eol
		for i in [0 .. els.length - 1[ do
			os.write(els[i].to_s.escape_to_csv(sep, esc, eol))
			os.write_char(sep)
		end
		os.write(els.last.to_s.escape_to_csv(sep, esc, eol))
		os.write(eol)
	end

	# Append the specified record.
	#
	# The representation of each cell is determined by `to_s`.
	fun write_line(line: Array[Object]) do
		var os = ostream
		var esc = delimiter
		var sep = separator
		var eol = eol
		for i in [0 .. line.length - 1[ do
			os.write(line[i].to_s.escape_to_csv(sep, esc, eol))
			os.write_char(sep)
		end
		os.write(line.last.to_s.escape_to_csv(sep, esc, eol))
		os.write(eol)
	end
end
lib/csv/csv.nit:167,1--223,3