If you need more programmatic control over the importation of CSV files than the Alpha5 standard functions provide, here's my latest attempt: CsvLineParse2(), a user UDF for use with Xbasic.
Input to this function is a line of csv field names or field data to parse. Output are the field names or data as a CRLF separated list, which is a format easily handled in Alpha5 Xbasic.
Example:
�"She sells, sea shells", "on the sea, shore",12345�
Becomes:
�She sells, sea shells
on the sea, shore
12345�
The csv conversion is done according to the RFC 4180 standard. More information here.
Particulary, the parsing must take account of commas within quoted fields and quotes within quoted fields. In the latter case quotes are escaped with a second quote, as in "". Example: �"""Hello"", she said."� is parsed as: �"Hello", she said.�
Commas can be field separators, so the code has to differentiate between comma field separators and commas that are not field separators. Also between quotes that are field delimiters and quotes that are just part of the field contents.
Not particularly efficient at 1.3 seconds per thousand records processed (on my PC with a Windows Experience Index of 5.6), this code does however work. I am not however yet claiming it is entirely bug free!
Preferably I would like to see the processing speed under 0.5 seconds per thousand records processed. But that will have to be for another day.
I have tested and so far been unable to find on the Internet any other code, including regular expressions, that is both suitable for use with Alpha5 and that meets the RFC 4180 standard. But I am still looking.
Attached is a zip file containing the Xbasic function CsvLineParse2(), a test Xbasic script and a test file of CSV data.
Any suggestions or improvements, just let me know.
Input to this function is a line of csv field names or field data to parse. Output are the field names or data as a CRLF separated list, which is a format easily handled in Alpha5 Xbasic.
Example:
�"She sells, sea shells", "on the sea, shore",12345�
Becomes:
�She sells, sea shells
on the sea, shore
12345�
The csv conversion is done according to the RFC 4180 standard. More information here.
Particulary, the parsing must take account of commas within quoted fields and quotes within quoted fields. In the latter case quotes are escaped with a second quote, as in "". Example: �"""Hello"", she said."� is parsed as: �"Hello", she said.�
Commas can be field separators, so the code has to differentiate between comma field separators and commas that are not field separators. Also between quotes that are field delimiters and quotes that are just part of the field contents.
Not particularly efficient at 1.3 seconds per thousand records processed (on my PC with a Windows Experience Index of 5.6), this code does however work. I am not however yet claiming it is entirely bug free!
Preferably I would like to see the processing speed under 0.5 seconds per thousand records processed. But that will have to be for another day.
I have tested and so far been unable to find on the Internet any other code, including regular expressions, that is both suitable for use with Alpha5 and that meets the RFC 4180 standard. But I am still looking.
Attached is a zip file containing the Xbasic function CsvLineParse2(), a test Xbasic script and a test file of CSV data.
Any suggestions or improvements, just let me know.
Comment