如何以类似于 NET FileHelpers 的方式读写 CSV?
有人知道我如何以类似于 NET FileHelpers 的方式导入/导出 csv、txt 文件,但使用 Delphi,考虑空格和引号,并以类似于 Excel 中 CSV 转义工作方式的方式处理传统 CSV 转义规则?
参考号链接 http://www.filehelpers.com/
如果您的答案往往是:“为什么这个懒人不这样做编写一个简单的 CSV 解析器”,花 5 分钟阅读,然后您就会知道为什么 CSV 解析并不简单:
Anyone know how I can import/export csv, txt files in a way similar to NET FileHelpers, but using Delphi, taking spaces and quotes into account and handling traditional CSV escaping rules in a manner similar to the way CSV escaping works in Excel?
ref. link http://www.filehelpers.com/
If your answer tends to be: "why this lazy guy dont write a simple CSV parser", consider this 5 minutes reading and then you will know why CSV parsing is not trivial:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我为 Jedi 项目编写了一个名为 TJvCsvDataSet 的数据集(类似于 TTable 的对象),它遵循所有 CSV 解析规则,其方式类似于 Excel 以及导入和导出 CSV 的各种数据库和报告工具使用的 CSV 解析规则。
您可以安装 JVCL,将 TJvCsvDataSet 拖放到表单上。
它还包含一个流类,可以非常快速地将文件加载到磁盘上,并使用 CSV 文件所需的正确转义规则逐行解析它,甚至包括在字段中编码的回车/换行代码的文件。
您只需将其放在表单上,然后像这样设置 FieldDefs 属性:
CsvFieldDef=ABC:%,DEF:#,GHI:$,....
对于整数、浮点、iso 日期时间和其他领域。它甚至允许您将宽字符串字段映射到 CSV 文件中的 utf8 字段。
有一个设计时属性编辑器可以让您不必使用上面的语法声明 CSV 字段定义,而是可以直接直观地选择列类型。
如果您没有设置 CSV 字段定义,它只会将文件中存在的任何内容映射到字符串类型字段。
绝地 JVCL:
http://jvcl.delphi-jedi.org/
JvCsvDataSet 文档:
http://help.delphi-jedi.org/unit.php?Id=3107
http://help.delphi-jedi.org/item.php?Id=174896
I wrote a Dataset (TTable-like object) for Jedi project called TJvCsvDataSet that follows all CSV parsing rules in a way similar to the CSV parsing rules used by Excel and various database and report tools that import and export CSVs.
You can install JVCL, drop a TJvCsvDataSet on your form.
It also contains a stream class that will very quickly load a file on disk, and parse it line by line, using the correct escape rules required for CSV files, even files that include carriage-return/line-feed codes encoded within a field.
You just drop it on your form, and set the FieldDefs property like this:
CsvFieldDef=ABC:%,DEF:#,GHI:$,....
There are special codes for integer, floating point, iso date-time, and other fields. It even allows you to map a wide-string field to a utf8 field in a CSV file.
There is a designtime property editor to save you from having to declare the CSV Field Defs using the syntax above, instead you can just pick visually what the column types are.
If you don't set up a CSV Field Def, it merely maps whatever exists in the file to string-type fields.
Jedi JVCL:
http://jvcl.delphi-jedi.org/
JvCsvDataSet Docs:
http://help.delphi-jedi.org/unit.php?Id=3107
http://help.delphi-jedi.org/item.php?Id=174896
这是非常基本的,但
TStringList
具有Delimiter
、DelimitedText
和QuoteChar
属性,可以解决其中一些问题。根据评论更新添加:不要被
CommaText
属性所诱惑,它对于与旧版 Delphi 的向后兼容性有一些令人惊讶的限制。It's pretty basic, but
TStringList
hasDelimiter
,DelimitedText
, andQuoteChar
properties, which address some of these issues.Updated to add, per comments: Don't be tempted by the
CommaText
property, which has some surprising limitations for backwards compatibility with archaic versions of Delphi.我的框架在 CsiTextStreamsUnt.pas 文件中有相关代码(请参阅 http://www.csinnovations.com/framework_delphi .htm)
My framework has code for this in the CsiTextStreamsUnt.pas file (see http://www.csinnovations.com/framework_delphi.htm)
这里我编写了一些读取 CSV 文件的代码,它也处理引号内的回车符。
Here some code I wrote that reads CSV files, it handles carriage returns inside quotes as well.
按照 VCL TXMLTransform 逻辑,我编写了一个 TCsvTransform 类帮助器,用于将 .csv 格式结构转换为 TClientDataSet 或从 TClientDataSet 转换。
有关 TCsvTransform 的更多详细信息,请参见 http://didier.cabale.free.fr/delphi。 htm#uCsvTransform。
注意:我设置了与 Warren 的 TJvCsvDataSet 相同的字段类型符号
Following the VCL TXMLTransform logic, I wrote a TCsvTransform class helper that translates a .csv format structure to /from a TClientDataSet.
For more details about TCsvTransform, cf http://didier.cabale.free.fr/delphi.htm#uCsvTransform.
NB: I set the same field type symbols as Warren's TJvCsvDataSet
我的职能
My functions
遇到了这个 Delphi CSV 文件和字符串读取器类今天在 CodeProject 上使用 Delphi 2009 及更高版本,我还没有尝试过,但从示例代码来看,它有点酷。由 Vladimir Nikitenko 编写,主类是 TnvvCSVReader。
Came across this Delphi CSV File and String Reader Classes for Delphi 2009 and later today on CodeProject, I've not tried it, but from the example code it's kinda cool. Written by Vladimir Nikitenko, the main class is TnvvCSVReader.