用 C++ 解析/写入 CSV 的首选库是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7827274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:33:10  来源:igfitidea点击:

what's the preferred library for CSV parsing/writing in C++?

c++parsingboostcsv

提问by daj

I've seen of this thread: How can I read and parse CSV files in C++?

我看过这个帖子:How can I read and parse CSV files in C++?

but it seems silly to reinvent the wheel for something as universal as a CSV parser. Boost has some routines which facilitate parsing, but doesn't have something out of the box.

但是为像 CSV 解析器这样通用的东西重新发明轮子似乎很愚蠢。Boost 有一些便于解析的例程,但没有开箱即用的功能。

Elsewhere, I see recommended libraries that are tied to .NET or otherwise platform specific. It's hard to believe that there isn't a preferred open C++ library to do something for this routine.

在其他地方,我看到了与 .NET 或其他特定平台相关的推荐库。很难相信没有一个首选的开放 C++ 库来为这个例程做些什么。

Any recommendations?

有什么建议吗?

回答by BJovke

There's no "preferred" library for CSV parsing because it is less effort to write your own parser in C++ than to download some library, read how to use it, link it with your code, potentially encounter some bugs, change code to suit your needs etc. CSV parsing is trivial, there are three things you need to do:

没有用于 CSV 解析的“首选”库,因为在 C++ 中编写自己的解析器比下载一些库、阅读如何使用它、将其与您的代码链接、可能遇到一些错误、更改代码以满足您的需要更省力等。CSV解析是微不足道的,你需要做三件事:

  • Detect field delimiter.
  • Detect row delimiter.
  • Skip delimiters which are inside quotes.
  • 检测字段分隔符。
  • 检测行分隔符。
  • 跳过引号内的分隔符。

Also there's an issue with file encoding, which delimiters you want to use, extra spaces and empty lines in file, and so on.

文件编码也存在问题,您要使用哪些分隔符、文件中的额外空格和空行等等。

If you still want to use "preferred" CSV parser then maybe you should completely skip coding in C++ and move to some other language.

如果您仍然想使用“首选”CSV 解析器,那么也许您应该完全跳过 C++ 编码并转向其他语言。