mirror of
https://github.com/FourierTransformer/ftcsv.git
synced 2024-11-19 19:54:23 +00:00
ftcsv.parse also returns table of column headers
output, headers = ftcsv.parse(...) -- headers = { 'Column 1', 'Column 2', ... }
This commit is contained in:
parent
7d5ac974b0
commit
62b1c1bd8f
@ -18,12 +18,12 @@ luarocks install ftcsv
|
||||
## Parsing
|
||||
### `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:
|
||||
```lua
|
||||
local ftcsv = require('ftcsv')
|
||||
local zipcodes = ftcsv.parse("free-zipcode-database.csv", ",")
|
||||
local zipcodes, headers = ftcsv.parse("free-zipcode-database.csv", ",")
|
||||
```
|
||||
|
||||
### Options
|
||||
|
@ -390,7 +390,7 @@ function ftcsv.parse(inputFile, delimiter, options)
|
||||
end
|
||||
|
||||
local output = parseString(inputString, inputLength, delimiter, i, headerField, fieldsToKeep)
|
||||
return output
|
||||
return output, headerField
|
||||
end
|
||||
|
||||
-- a function that delimits " to "", used by the writer
|
||||
|
@ -42,6 +42,13 @@ describe("csv features", function()
|
||||
assert.are.same(expected, actual)
|
||||
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()
|
||||
local expected = {}
|
||||
expected[1] = {}
|
||||
|
Loading…
Reference in New Issue
Block a user