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
## Features
* Can now parse files line by line in a fixed-size reading mode
* Now has an option to ignore quotes when parsing
## Improvements
* Speed increases in vanilla Lua and LuaJIT (benchmarks updated!)
* Refactored code for easier maintenance
## Bugfixes
* Better handling of multiple escaped quotes in vanilla lua (thanks @fredrikj83#25)
Below you can find a more detailed explanation of some of the errors that can be encountered while using ftcsv. For parsing, examples of these files can be found in /spec/bad_csvs/
3
3
4
4
5
5
6
-
##Parsing
6
+
##Parsing
7
7
Note: `[row_number]` indicates the row number of the parsed lua table. As such, it will be one off from the line number in the csv. However, for header-less files, the row returned *will* match the csv line number.
8
8
9
9
| Error Message | Detailed Explanation |
@@ -12,4 +12,9 @@ Note: `[row_number]` indicates the row number of the parsed lua table. As such,
12
12
| ftcsv: Cannot parse a file which contains empty headers | If a header field contains no information, then it can't be parsed <br> (ex: `Name,City,,Zipcode`) |
13
13
| ftcsv: too few columns in row [row_number]| The number of columns is less than the amount in the header after transformations (renaming, keeping certain fields, etc) |
14
14
| ftcsv: too many columns in row [row_number]| The number of columns is greater than the amount in the header after transformations. It can't map the field's count with an existing header. |
15
-
| ftcsv: File not found at [path]| When loading, lua can't open the file at [path]|
15
+
| ftcsv: File not found at [path]| When loading, lua can't open the file at [path]|
16
+
| ftcsv: fieldsToKeep only works with header-less files when using the 'rename' functionality | when dealing with header-less files, you can only use the fieldsToKeep if you use rename. The fields are limited after the renaming happens |
17
+
| ftcsv: bufferSize needs to be larger to parse this file | The buffer size selected is too small to parse the file. It must be at least the length of the longest row (but, for performance, should probably be a bit larger). |
18
+
| ftcsv: parseLine currently doesn't support loading from string |`parseLine` relies on reading a file a few bytes at a time and currently doesn't work on strings |
19
+
| ftcsv: bufferSize can only be specified using 'parseLine'. When using 'parse', the entire file is read into memory | bufferSize can't be specified for parse, it can only be specified for parseLine |
It works well for CSVs that can easily be fully loaded into memory (easily up to a hundred MB) and correctly handles `\n` (LF), `\r` (CR) and `\r\n` (CRLF) line endings. It has UTF-8 support, and will strip out the BOM if it exists. ftcsv can also parse headerless csv-like files and supports column remapping, file or string based loading, and more!
7
-
8
-
Currently, there isn't a "large" file mode with proper readers for ingesting large CSVs using a fixed amount of memory, but that is in the works in [another branch!](https://github.com/FourierTransformer/ftcsv/tree/parseLineIterator)
9
-
10
-
It's been tested with LuaJIT 2.0/2.1 and Lua 5.1, 5.2, and 5.3
11
-
4
+
ftcsv is a fast csv library written in pure Lua. It's been tested with LuaJIT 2.0/2.1 and Lua 5.1, 5.2, and 5.3
12
5
6
+
It features two parsing modes, one for CSVs that can easily be loaded into memory (up to a few hundred MBs depending on the system), and another for loading files using an iterator - useful for manipulating large files or processing during load. It correctly handles most csv (and csv-like) files found in the wild, from varying line endings (Windows, Linux, and OS9), UTF-8 BOM support, and odd delimiters. There are also various options that can tweak how a file is loaded, only grabbing a few fields, renaming fields, and parsing header-less files!
13
7
14
8
## Installing
15
9
You can either grab `ftcsv.lua` from here or install via luarocks:
There are two main parsing methods: `ftcv.parse` and `ftcsv.parseLine`.
18
+
`ftcsv.parse` loads the entire file and parses it, while `ftcsv.parseLine` is an iterator that parses one line at a time.
24
19
25
-
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).
`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 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).
The following are optional parameters passed in via the third argument as a table. For example if you wanted to `loadFromString` and not use `headers`, you could use the following:
`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
+
32
+
If specifying `bufferSize` there are a couple of things to remember:
33
+
*`bufferSize` must be at least the length of the longest row.
34
+
* If `bufferSize` is too small, an error is returned.
35
+
* If `bufferSize` is the length of the entire file, all of it will be read and returned one line at a time (performance is roughly the same as `ftcsv.parse`).
forzipcodeinftcsv.parseLine("free-zipcode-database.csv", ",") do
41
+
print(zipcode.Zipcode)
42
+
print(zipcode.State)
43
+
end
37
44
```
45
+
46
+
47
+
### Options
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
+
50
+
The following are optional parameters passed in via the third argument as a table.
38
51
-`loadFromString`
39
52
40
53
If you want to load a csv from a string instead of a file, set `loadFromString` to `true` (default: `false`)
local actual = ftcsv.parse("a,b,c\r\napple,banana,carrot\r\n", ",", options)
65
78
```
66
79
80
+
Also Note: If you apply a function to the headers via headerFunc, and want to select fields from fieldsToKeep, you need to have what the post-modified header would be in fieldsToKeep.
81
+
82
+
-`ignoreQuotes`
83
+
84
+
If `ignoreQuotes` is `true`, it will leave all quotes in the final parsed output. This is useful in situations where the fields aren't quoted, but contain quotes, or if the CSV didn't handle quotes correctly and you're trying to parse it.
85
+
86
+
```lua
87
+
local options = {loadFromString=true, ignoreQuotes=true}
88
+
local actual = ftcsv.parse('a,b,c\n"apple,banana,carrot', ",", options)
89
+
```
90
+
67
91
-`headerFunc`
68
92
69
93
Applies a function to every field in the header. If you are using `rename`, the function is applied after the rename.
ftcsv can also take a lua table and turn it into a text string to 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:
129
+
`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:
102
130
```lua
103
131
localfileOutput=ftcsv.encode(users, ",")
104
132
localfile=assert(io.open("ALLUSERS.csv", "w"))
@@ -116,54 +144,53 @@ file:close()
116
144
```
117
145
118
146
119
-
## Error Handling
120
-
ftcsv returns a bunch of errors when passed a bad csv file or incorrect parameters. You can find a more detailed explanation of the more cryptic errors in [ERRORS.md](ERRORS.md)
121
147
148
+
## Error Handling
149
+
ftcsv returns a litany of errors when passed a bad csv file or incorrect parameters. You can find a more detailed explanation of the more cryptic errors in [ERRORS.md](ERRORS.md)
122
150
123
151
## Benchmarks
124
152
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:
125
153
126
-
### 20 MB file, every field is double quoted (ftcsv optimal lua case\*)
[LuaCSV](http://lua-users.org/lists/lua-l/2009-08/msg00012.html) was also tried, but usually errored out at odd places during parsing.
147
174
148
175
NOTE: times are measured using `os.clock()`, so they are in CPU seconds. Each test was run 30 times in a randomized order. The file was pre-loaded, and only the csv decoding time was measured.
149
176
150
-
Benchmarks were run under ftcsv 1.1.6
177
+
Benchmarks were run under ftcsv 1.2.0
151
178
152
179
## Performance
153
-
We did some basic testing and found that in lua, if you want to iterate over a string character-by-character and look for single chars, `string.byte` performs faster than `string.sub`. This is especially true for LuaJIT. As such, in LuaJIT, ftcsv iterates over the whole file and does byte compares to find quotes and delimiters. However, for pure lua, `string.find`is used to find quotes but `string.byte` is used everywhere else as the CSV format in its proper form will have quotes around fields. If you have thoughts on how to improve performance (either big picture or specifically within the code), create a GitHub issue - I'd love to hear about it!
180
+
I did some basic testing and found that in lua, if you want to iterate over a string character-by-character and compare chars, `string.byte` performs faster than `string.sub`. As such, ftcsv iterates over the whole file and does byte compares to find quotes and delimiters and then generates a table from it. When using vanilla lua, it proved faster to use `string.find`instead of iterating character by character (which is faster in LuaJIT), so ftcsv accounts for that and will perform the fastest option that is availble. If you have thoughts on how to improve performance (either big picture or specifically within the code), create a GitHub issue - I'd love to hear about it!
154
181
155
182
156
183
## Contributing
157
184
Feel free to create a new issue for any bugs you've found or help you need. If you want to contribute back to the project please do the following:
158
185
159
-
0. If it's a major change (aka more than a quick bugfix), please create an issue so we can discuss it!
160
-
1. Fork the repo
161
-
2. Create a new branch
162
-
3. Push your changes to the branch
163
-
4. Run the test suite and make sure it still works
164
-
5. Submit a pull request
165
-
6. Wait for review
166
-
7. Enjoy the changes made!
186
+
1. If it's a major change (aka more than a quick bugfix), please create an issue so we can discuss it!
187
+
2. Fork the repo
188
+
3. Create a new branch
189
+
4. Push your changes to the branch
190
+
5. Run the test suite and make sure it still works
0 commit comments