mirror of
https://github.com/FourierTransformer/ftcsv.git
synced 2024-11-19 19:54:23 +00:00
Merge pull request #7 from Oozlum/return_headers
ftcsv.parse also returns table of column headers
This commit is contained in:
commit
29675f4d61
@ -18,12 +18,12 @@ luarocks install ftcsv
|
|||||||
## Parsing
|
## Parsing
|
||||||
### `ftcsv.parse(fileName, delimiter [, options])`
|
### `ftcsv.parse(fileName, delimiter [, options])`
|
||||||
|
|
||||||
ftcsv will load the entire csv file into memory, then parse it in one go, returning a lua table with the parsed data. It has only two required parameters - a file name and delimiter (limited to one character). A few optional parameters can be passed in via a table (examples below).
|
ftcsv will load the entire csv file into memory, then parse it in one go, returning a lua table with the parsed data and a lua table containing the column headers. It has only two required parameters - a file name and delimiter (limited to one character). A few optional parameters can be passed in via a table (examples below).
|
||||||
|
|
||||||
Just loading a csv file:
|
Just loading a csv file:
|
||||||
```lua
|
```lua
|
||||||
local ftcsv = require('ftcsv')
|
local ftcsv = require('ftcsv')
|
||||||
local zipcodes = ftcsv.parse("free-zipcode-database.csv", ",")
|
local zipcodes, headers = ftcsv.parse("free-zipcode-database.csv", ",")
|
||||||
```
|
```
|
||||||
|
|
||||||
### Options
|
### Options
|
||||||
|
@ -390,7 +390,7 @@ function ftcsv.parse(inputFile, delimiter, options)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local output = parseString(inputString, inputLength, delimiter, i, headerField, fieldsToKeep)
|
local output = parseString(inputString, inputLength, delimiter, i, headerField, fieldsToKeep)
|
||||||
return output
|
return output, headerField
|
||||||
end
|
end
|
||||||
|
|
||||||
-- a function that delimits " to "", used by the writer
|
-- a function that delimits " to "", used by the writer
|
||||||
|
@ -42,6 +42,13 @@ describe("csv features", function()
|
|||||||
assert.are.same(expected, actual)
|
assert.are.same(expected, actual)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("should return a table with column headers", function()
|
||||||
|
local expected = { 'd', 'e', 'f' }
|
||||||
|
local options = {loadFromString=true, rename={["a"] = "d", ["b"] = "e", ["c"] = "f"}}
|
||||||
|
local _, actual = ftcsv.parse("a,b,c\r\napple,banana,carrot", ",", options)
|
||||||
|
assert.are.same(expected, actual)
|
||||||
|
end)
|
||||||
|
|
||||||
it("should handle renaming multiple fields to the same out value", function()
|
it("should handle renaming multiple fields to the same out value", function()
|
||||||
local expected = {}
|
local expected = {}
|
||||||
expected[1] = {}
|
expected[1] = {}
|
||||||
|
Loading…
Reference in New Issue
Block a user