java CSV 文件和逗号里面的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14707481/
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-10-31 17:06:32  来源:igfitidea点击:

CSV file and comma inside values

javacsvcomma

提问by Eddy

Possible Duplicate:
Java: splitting a comma-separated string but ignoring commas in quotes

可能的重复:
Java:拆分逗号分隔的字符串但忽略引号中的逗号

Is there a way to handle commas inside values in a csv file?

有没有办法处理 csv 文件中值内的逗号?

For example if there's a company name, Some Company, Inc.. The usual parsing will interpret the comma between company and Inc as separator between values, and this throws everything off.

例如,如果有一个公司名称,Some Company, Inc.。通常的解析会将 company 和 Inc 之间的逗号解释为值之间的分隔符,这会将所有内容都抛之脑后。

I have no control over the csv file -- I need to work with it the way it is. Is there a solution in Java?

我无法控制 csv 文件——我需要按原样使用它。Java中有解决方案吗?

EDIT: the solution can be found here

编辑:可以在此处找到解决方案

回答by jspboix

If the CSV file was generated with quoting you should use them. Otherwise you'll have to create a tool that automatically escapes the comma based on some recognizable pattern before processing the file.

如果 CSV 文件是通过引用生成的,您应该使用它们。否则,您将不得不创建一个工具,在处理文件之前根据一些可识别的模式自动转义逗号。

回答by Jayan

Use OpenCSV, that is a widely used library to handle csv format.

使用OpenCSV,这是一个广泛使用的库来处理 csv 格式。

回答by iDev

I usually use csv files separated with semi-colon instead of coma. Basically a csv file is a text file with separators. You can use many separators. For example, you can test it by creating a text file, add few items separated by semi-colon and then change the file extension to csv.

我通常使用用分号而不是逗号分隔的 csv 文件。基本上一个 csv 文件是一个带有分隔符的文本文件。您可以使用许多分隔符。例如,您可以通过创建一个文本文件,添加几个以分号分隔的项目,然后将文件扩展名更改为 csv 来测试它。

Here's a link giving more information

这是一个提供更多信息的链接