读取csv文件时出现问题 [英] Problem while reading csv file

查看:265
本文介绍了读取csv文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在将csv文件的内容读取到字符串数组.
我的问题是文件的内容在某些字符串中包含,".
这些字符串显示在"之间的文件中.
现在的问题是我正在使用'',''拆分每行,而在其字符串中包含'',''的行则显示更多列.
下面我给出这样的字符串的一个例子:

Hi All,

I am reading content of a csv file to a string array.
My problem is the content of file contains '','' in some of strings.
Those strings are shown in file in between "".
Now problem is i am splitting each line using '','' and those lines which contains '','' in its strings they are displaying more columns.
Below i am giving an example of such string:

abc,28384,"this,comma",8673,xyz,


有人可以告诉我如何分割这条线吗?

谢谢,
Nagendra.


Can anybody tell me how can i split such line.

Thanks,
Nagendra.

推荐答案

您不能使用String.Split进行此操作.

您必须使用正则表达式来分割字符串或编写我们的解析器来跟踪打开和关闭引号.

我建议使用正则表达式方法.您可以在
You can''t use String.Split to do this.

You''ll have to eitehr use a regex to split the strings or write your our parser that keeps track of open and closing quote marks.

I suggest the regex method. You can find a lot of information on it in these[^].


使用此正则表达式
".*"|[^,]+
Use this regex
".*"|[^,]+


算法不是那么简单.我碰巧有完整源代码的解决方案,只有定界符是空白.我这样做是为了创建一种工具来测试命令行,该命令行中包含带引号保护的空格的参数.我想不使用实际命令行进行测试.您只需要用逗号替换空格字符.

让我展示如何找到它.转到5月文章:
基于枚举的命令行实用程序 [CommandLineSimulationUtility.下载源代码并在测试/演示应用程序中找到此类.

享受.

—SA
The algorithm is not that simple. I happened to have the solution with full source code, only the delimiter is blank space. I''ve done this to created a facility to test command line with parameters containing blank spaces guarded with quotation mark. I wanted to do testing not using actual command line. You only need to replace blank space character with comma.

Let me show how to locate it. Go to may article: Enumeration-based Command Line Utility[^], locate the section "6. CommandLine Testing" — it explains the class CommandLineSimulationUtility. Download the source code and locate this class in a test/demo application.

Enjoy.

—SA


这篇关于读取csv文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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