FasterCSV:在接受文件之前检查文件是否无效 - 有更简单的方法吗?
我在 Ruby on Rails 应用程序上使用 FasterCSV,目前如果文件无效,它会抛出异常。
我查看了 FasterCSV 文档,似乎如果我使用 FasterCSV::parse 带有一个块,它会一次读取文件一行,而不分配太多内存。如果存在任何类型的错误,它将抛出 FasterCSV::MalformedCSV 异常在文件上。
我已经实现了一个自定义解决方案,但我不确定它是最好的解决方案(请参阅下面的我的答案)。我有兴趣了解替代方案
I'm using FasterCSV on a Ruby on Rails application and currently it throws an Exception if the file is invalid.
I've looked over the FasterCSV doc, and it seems that if I use FasterCSV::parse with a block, it'll read the file one line at a time, without allocating too much memory. It'll throw a FasterCSV::MalformedCSV exception if there is any kind of error on the file.
I've implemented a custom solution, but I'm not sure it's the best possible one (see my answer below). I'd be interested in knowing alternatives
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我目前的解决方案。我真的很想了解改进/替代方案。
我使用这样的方法:
This is my current solution. I'm really interested in knowing improvements / alternatives.
I use that method like this:
我昨天做了一些测试,结果发现我的解决方案不太有效;在实现第一个
is_valid
后,我不断在有效的 CSV 上获取空数组。我不确定这是 FasterCSV 缓存问题还是我的代码中的某些问题,而且我不知道它是否与我的测试设置有关,但我决定改为实现一个safe_parse
:这将如果文件有效,则返回已解析的数组,否则返回 nil。然后我可以按如下方式实现我的验证:
我想可以实现一个接受块并逐行解析文件的
safe_parse
,但对于我的目的来说,这个简单的实现就足够了,而且它在所有情况下都有效。I made some tests yesterday and it turns out that my solution didn't quite work; I kept getting empty arrays on valid CSVs after implementing the first
is_valid
. I'm not sure whether it's a FasterCSV caching issue or something in my code, and I don't know if it's related with my test setup, but I decided to go implement asafe_parse
instead:This will return a parsed array if the file is valid, or
nil
otherwise. I could then implement my validations as follows:I guess it would be possible to implement a
safe_parse
that accepts a block and parses the file line by line, but for my purposes this simple implementation was enough, and it works in all cases.我假设您想要解析 CSV 并对解析的结果执行一些操作。最坏的情况是您的 CSV 有效并且您再次解析该文件。我会写这样的东西来隐藏解析的结果,这样你只需要解析 CSV 一次:
然后:
在上面的情况下,你可能想将东西移到另一个类中,该类负责与 FasterCSV 通信并隐藏解析的结果,因为我认为我的示例不是线程安全的:)
I assume you want to parse the CSV and do something with the parsed results. Worst case is that your CSV is valid and that you parse the file again. I would write something like this to stash away the parsed result so you only have to parse the CSV once:
And then:
In the above case, you might want to move stuff into a different class that takes care of communicating with FasterCSV and stashing away the parsed result, because I don't think my example is thread safe :)