Coder Social home page Coder Social logo

gscsvsupport's Introduction

GsCsvSupport

Handling of csv-files for GemStone/S

This package currently contains the classes CsvReader and CsvWriter.

CsvReader

CsvReader is a self-contained class with no dependencies other than GemStone/S kernel classes. It allows performant reading of very large amount of csv-data from a file stream.

CsvReader will auto-detect the used line-ending from either lf (Unix style), cr (Classic Mac style) or crlf (Windows style). However, consitent line-endings within a single file are expected.

The used delimiter can optionally be specified with useDelimiter:, otherwise an auto-detection is tried from the commonly used separators tab, comma, semicolon, pipe, tilde or colon.

CsvReader auto-detects the encoding of the file. UTF-8 and ANSI encoding is supported. If the file starts with an UTF-8 BOM, the file is clearly detected as UTF-8 encoded. If no UTF-8 BOM is given, auto-detection will be done based on the file's content, paying special attention to german umlauts and the Euro sign €. In case of an ANSI file, Windows-1252 is assumed rather than ISO-8859-1, therefore octet 0x80 will be interpreted as Euro sign € and automatically transcoded to its unicode.

CsvReader respects RFC 4180 and can handle properly escaped files, hence double-quotes, the used separator and line breaks can be part of the file's content without harming the file's structue.

Usage of CsvReader is very straightforward and illustrated by the following example:

	| reader lines headerArray |
	lines := OrderedCollection new.

	reader := CsvReader newOnServerFile: '/path/to/file.csv'.

	"optional: Set the delimiter to omit auto-detection."
	reader useDelimiter: $;.

	"optional: Read a single (header) line, e.g. to verify existence of expected column headers."
	headerArray := reader readHeaderFromLine: 1.

	"optional: Specify the first line to read. Default is 1."
	reader firstLineToRead: 2.

	"Specify a two-argument-block, which will be evaluated for each read line.
	First argument is an array holding the cells of the line; second argument is the line number."
	reader linesWithIndexDo: [:lineArray :index | lines add: index -> lineArray].

	"Start reading the file."
	reader read.

	^lines

CsvWriter

CsvWriter is a self-contained class with no dependencies other than GemStone/S kernel classes.

	| csvWriter |
	csvWriter := CsvWriter newOnServerFile: '~/CsvWriterTest.csv'.
	csvWriter
		delimiter: $;; "optional; default is comma"
		nilString: 'I represent a nil value'; "optional; default is an empty string"
		beUtf8; "optional; default is not to encode to utf8"
		openFile.
	csvWriter addLineArray: #('col1' 'col2' 'col3' 4 5 nil 'Text containing " a quotaion mark').
	csvWriter closeFile

gscsvsupport's People

Contributors

ramauers avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.