CSV 解析器
Input text and output objects or Arrays
This package is a parser converting CSV text input into arrays or objects. It implements the Node.js stream.TransformAPI. It also provides a simple callback-based API for convenience. It is both extremely easy to use and powerful. It was first released in 2010 and is used against big data sets by a large community.
Source code for this project is available on GitHub.
功能
- Follow the Node.js streaming API
- Simplicity with the optional callback API
- Support delimiters, quotes, escape characters and comments
- Line breaks discovery
- Support big datasets
- Complete test coverage and samples for inspiration
- 没有外部的依赖关系
- 可以与
csv-generate
,stream-transform
以及csv-stringify
联合使用
使用
Run npm install csv
to install the full CSV package or run npm install csv-parse
if you are only interested by the CSV parser.
Use the callback style API for simplicity or the stream based API for scalability. You may also mix the two styles. For example, the fs_read.js example pipe a file stream reader and get the results inside a callback.
回调 API
签名: parse(data, [options], callback)
Node.js 流 API
签名: parse([options], [callback])
For additional usage and example, you may refer to example page, the "samples" folder and the "test" folder.
选项
- delimiter (char) Set the field delimiter. One character only, defaults to comma.
- rowDelimiter (chars|constant) String used to delimit record rows or a special value; special constants are 'auto', 'unix', 'mac', 'windows', 'unicode'; defaults to 'auto' (discovered in source or 'unix' if no source is specified).
- quote (char) Optionnal character surrounding a field, one character only, defaults to double quotes.
- escape (char) Set the escape character, one character only, defaults to double quotes.
- columns (array|boolean|function) List of fields as an array, a user defined callback accepting the first line and returning the column names or true if autodiscovered in the first CSV line, default to null, affect the result data set in the sense that records will be objects instead of arrays.
- comment (char) Treat all the characters after this one as a comment, default to '#'.
- objname (string) Name of header-record title to name objects by.
- relax (boolean) Preserve quotes inside unquoted field.
- skip_empty_lines (boolean) Dont generate empty values for empty lines.
- trim (boolean) If true, ignore whitespace immediately around the delimiter, defaults to false. Does not remove whitespace in a quoted field.
- ltrim (boolean) If true, ignore whitespace immediately following the delimiter (i.e. left-trim all fields), defaults to false. Does not remove whitespace in a quoted field.
- rtrim (boolean) If true, ignore whitespace immediately preceding the delimiter (i.e. right-trim all fields), defaults to false. Does not remove whitespace in a quoted field.
- auto_parse (boolean) If true, the parser will attempt to convert read data types to native types.
- auto_parse_date (boolean) If true, the parser will attempt to convert read data types to dates. It requires the "auto_parse" option.
所有选项可选.
内部属性
Those properties are for internal usage but may be considered usefull to the final user in some situations. They are accessible from the intance returned by the parse function.
- count (number) Internal counter of records being processed.
- lines (number) The number of lines encountered in the source dataset.
- is_int (regexp, function) The regular expression or function used to determine if a value should be cast to an integer.
- is_float (regexp, function) The regular expression or function used to determine if a value should be cast to a float.
迁移
Most of the generator is imported from its parent project CSV in an effort to split it between the generator, the parser, the transformer and the stringifier.
The "record" has disappeared, you are encouraged to use the "readable" event conjointly with the "read" function as documented above and in the Stream API.