让 FileHelpers 2.0 处理包含多余逗号的 CSV 文件 [英] Getting FileHelpers 2.0 to handle CSV files with excess commas

查看:18
本文介绍了让 FileHelpers 2.0 处理包含多余逗号的 CSV 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 FileHelpers 库 (v2.0.0.0) 来解析 CSV 文件.CSV 文件被映射到一个具有一些公共属性的类,假设有 N.问题在于,默认情况下,FileHelpers 似乎无法正确处理用户指定的 CSV 文件超过 N-1 个逗号的情况.其余的逗号只是附加到最后一个属性值.

I am currently using the FileHelpers library (v2.0.0.0) to parse a CSV file. The CSV file is mapped to a class that has a handful of public properties, let's say there are N. The problem is that, by default, FileHelpers doesn't seem to correctly handle cases where the user specifies a CSV file that has more than N-1 commas. The remaining commas just get appended to the last property value.

我认为这必须通过 FileHelpers 的属性 进行配置,但我什么也没看到这将忽略记录中没有匹配属性的字段.

I figured this must be configurable via FileHelpers' attributes, but I didn't see anything that would ignore fields that don't have a matching property in the record.

我查看了 RecordCondition s,但使用了类似 ExcludeIfEnds(",") 看起来如果它以逗号结尾,它将完全跳过该行,但我只是希望它们被剥离.

I looked into the RecordConditions, but using something like ExcludeIfEnds(",") looks like it will skip the line entirely if it ends with a comma, but I just want them stripped.

我唯一的办法可能是预处理文件并去除任何尾随逗号,这完全没问题,但我想知道 FileHelpers 是否也可以这样做,也许我只是没有在文档中看到它.

It's possible that my only recourse is to pre-process the file and strip any trailing commas, which is totally fine, but I wanted to know if FileHelpers can do this as well, and perhaps I'm just not seeing it in the docs.

推荐答案

只是一个黑客/解决方法的想法:您可以创建一个名为ExtraCommas"的属性并将其添加到您的类中,以便额外的逗号在那里序列化并不在你的对象的真实属性中......

Just an idea for a hack / workaround: you could create a property called "ExtraCommas" and add it to your class, so that extra commas are serialized there and not in the real properties of your object...

这篇关于让 FileHelpers 2.0 处理包含多余逗号的 CSV 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆