【发布时间】:2017-09-30 04:18:14
【问题描述】:
我将 csv 文件组织为 {Id,OwnerUserId,CreationDate,ClosedDate,Score,Title,Body}
我正在尝试使用特殊字符拆分数据。这里以我的数据为例:
1040,254,2008-08-04T05:45:22Z,NA,42,How do I delete a file which is locked by another process in C#?,"<p>I'm looking for a way to delete a file which is locked by another process using C#. I suspect the method must be able to find which process is locking the file (perhaps by tracking the handles, although I'm not sure how to do this in C#) then close that process before being able to complete the file delete using <code>File.Delete()</code>.</p>
"
1070,236,2008-08-04T07:34:44Z,NA,17,Process size on UNIX,"<p>What is the correct way to get the process size on <code>Solaris, HP-UX</code> and <code>AIX</code>? Should we use <code>top</code> or <code>ps -o vsz</code> or something else?</p>
似乎数据以逗号分隔,但有时标题或正文也包含逗号,并且似乎在记录之间有“。所以我怎样才能达到以下结果:
Array 1 { 1040,254,2008-08-04T05:45:22Z,NA,42, title data, body data }
Array 2 { 1070,236,2008-08-04T07:34:44Z,NA,17, title data, body data }
我试过了
String[] tokens = line.split(",(?![^<>]*</>)")
但没用
【问题讨论】:
-
你为什么要重新发明轮子?已经有许多 CSV 框架可以处理有效的 CSV 数据。只需使用其中之一。
-
如果 csv 文件格式正确(符合 CSV 规范),您可能应该使用 CSV 解析器而不是尝试手动解析它。