You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+37-19
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ luarocks install ftcsv
17
17
There are two main parsing methods: `ftcv.parse` and `ftcsv.parseLine`.
18
18
`ftcsv.parse` loads the entire file and parses it, while `ftcsv.parseLine` is an iterator that parses one line at a time.
19
19
20
-
### `ftcsv.parse(fileName, [, options])`
20
+
### `ftcsv.parse(fileName [, options])`
21
21
`ftcsv.parse` 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 one required parameter - the file name. A few optional parameters can be passed in via a table (examples below).
`ftcsv.parseLine` will open a file and read `options.bufferSize` bytes of the file. `bufferSize` defaults to 2^16 bytes (which provides the fastest parsing on most unix-based systems), or can be specified in the options. `ftcsv.parseLine` is an iterator and returns one line at a time. When all the lines in the buffer are read, it will read in another `bufferSize` bytes of a file and repeat the process until the entire file has been read.
31
31
32
32
If specifying `bufferSize` there are a couple of things to remember:
@@ -48,7 +48,7 @@ end
48
48
The options are the same for `parseLine` and `parse`, with the exception of `loadFromString` and `bufferSize`. `loadFromString` only works with `parse` and `bufferSize` can only be specified for `parseLine`.
49
49
50
50
The following are optional parameters passed in via the third argument as a table.
51
-
-`delimeter`
51
+
-`delimiter`
52
52
53
53
If your file doesn't use the comma character as the delimiter, you can specify your own. It is limited to one character and defaults to `,`
`ftcsv.encode` takes in a lua table and turns it into a text string that can be written to a file. It has two required parameters, an inputTable and a delimiter. You can use it to write out a file like this:
136
+
`ftcsv.encode` takes in a lua table and turns it into a text string that can be written to a file. You can use it to write out a file like this:
137
137
```lua
138
-
localfileOutput=ftcsv.encode(users, ",")
138
+
localusers= {
139
+
{name="alice", fruit="apple"},
140
+
{name="bob", fruit="banana"},
141
+
{name="eve", fruit="pear"}
142
+
}
143
+
localfileOutput=ftcsv.encode(users)
139
144
localfile=assert(io.open("ALLUSERS.csv", "w"))
140
145
file:write(fileOutput)
141
146
file:close()
142
147
```
143
148
144
149
### Options
150
+
-`delimiter`
151
+
152
+
by default the encoder uses a `,` as a delimiter. The delimiter can be changed by setting a value for `delimiter`
if `fieldsToKeep` is set in the encode process, only the fields specified will be written out to a file. The `fieldsToKeep` will be written out in the order that is specified.
148
161
149
162
```lua
150
-
local output = ftcsv.encode(everyUser, ",", {fieldsToKeep={"Name", "Phone", "City"}})
163
+
local output = ftcsv.encode(everyUser, {fieldsToKeep={"Name", "Phone", "City"}})
151
164
```
152
165
153
166
-`onlyRequiredQuotes`
154
167
155
168
if `onlyRequiredQuotes` is set to `true`, the output will only include quotes around fields that are quotes, have newlines, or contain the delimter.
Startingwithversion1.4.0, thedelimiternolongerrequiredasthesecondargument. **Butdon't worry,** ftcsv remains backwards compatible! We check the argument types and adjust parsing as necessary. There is no intention to remove this backwards compatibility layer, so your existing code should just keep on working!
The delimiter as the second argument will always take precedent if both are provided.
192
+
193
+
165
194
## Benchmarks
166
195
We ran ftcsv against a few different csv parsers ([PIL](http://www.lua.org/pil/20.4.html)/[csvutils](http://lua-users.org/wiki/CsvUtils), [lua_csv](https://github.com/geoffleyland/lua-csv), and [lpeg_josh](http://lua-users.org/lists/lua-l/2009-08/msg00020.html)) for lua and here is what we found:
167
196
@@ -207,17 +236,6 @@ Feel free to create a new issue for any bugs you've found or help you need. If y
207
236
8. Enjoy the changes made!
208
237
209
238
210
-
##Delimiternolongerrequiredasof1.4.0!
211
-
Startingwithversion1.4.0, thedelimiternolongerrequiredasthesecondargument. **Butdon't worry,** ftcsv remains backwards compatible! We check the argument types and adjust parsing as necessary. There is no intention to remove this backwards compatibility layer, so you can always enjoy your up-to-date lightning fast CSV parser!
The delimiter as the second argument will always take precedent if both are provided.
219
-
220
-
221
239
## Licenses
222
240
- The main library is licensed under the MIT License. Feel free to use it!
223
241
- Some of the test CSVs are from [csv-spectrum](https://github.com/maxogden/csv-spectrum) (BSD-2-Clause) which includes some from [csvkit](https://github.com/wireservice/csvkit) (MIT License)
0 commit comments