C#在字段中使用额外的逗号解析文件 [英] C# parse file with extra commas in a field

查看:74
本文介绍了C#在字段中使用额外的逗号解析文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用C#解析字段中带有额外逗号的文件,并将其替换为竖线(|)。请查找示例数据和我尝试过的代码。在这种情况下,此数据没有任何文本限定符,这是替换列分隔符的最佳选择。

Hi, I am using C# to parse a file with extra commas in a field and replace it with pipe(|). Please find sample data and the code I tried. This data does not have any text qualifier in such a case what would be my best bet to replace the column delimiter.

示例数据:

发票编号,发票状态,发票服务日期,已开票的发票日期,打印/提交时的发票保留,发票分支,发票余额调整,发票余额付款,发票余额余额,患者姓氏,患者名字,患者ID,政策
付款人姓名,发票明细项目ID,发票明细项目名称,发票明细余额,发票详情数量,发票明细项目组

12345,Open,3/31/2009 12:00:00 AM,5/26/2009 8:09:02 AM,False,Test,($ 346.23) ,$ 0.00 $ 346.23,史密斯,约翰,12344,[患者],试验1234,5"" X8""多轴支柱支架,Therafin,31552,$ 51.12,1,轮椅配件

Invoice Number,Invoice Status,Invoice Date of Service,Invoice Date Opened,Invoice Hold from printing/submission,Invoice Branch,Invoice Balances Adjustments,Invoice Balances Payments,Invoice Balances Balance,Patient Last Name,Patient First Name,Patient ID,Policy Payor Name,Invoice Detail Item ID,Invoice Detail Item Name,Invoice Detail Balance,Invoice Detail Qty,Invoice Detail Item Group
12345,Open,3/31/2009 12:00:00 AM,5/26/2009 8:09:02 AM,False,Test,($346.23),$0.00,$346.23,Smith,John,12344,[Patient],Test-1234,5""x8"" Multi-Axis Stump Support, Therafin, 31552,$51.12,1,Wheelchair Accessories

以下数据全部在一个字段中。

The below data is all in one field.

' 5"" x8""多轴树桩支持,Therafin,31552 '

代码:

	public void Main()
        {
            System.IO.StreamReader sr = new System.IO.StreamReader(@"Z:\file.csv");
            string str1=(char)34+","+(char)34;
            string str2=(char)34+"|"+(char)34;
            string strContent = sr.ReadToEnd().Replace(str1, str2);
            sr.Close();
            sr.Dispose();

            System.IO.StreamWriter sw = new System.IO.StreamWriter(@"Z:\file.csv",false);
            sw.Write(strContent);
            sw.Flush();
            sw.Close();
            sw.Dispose();
        }

谢谢

SQLEnthusiast

SQLEnthusiast

推荐答案

你真的,真的,真的需要让他们修复数据。其中可以包含逗号但是没有附加分隔符的逗号分隔的数据是错误的。这是非常糟糕的数据。我假设你没有做出那个决定,如果你做了那么我道歉但是b $ b我认为你同意他们做错了。

You really, really, really need to get them to fix the data. Data that can have commas in it that is delimited by commas without an additional delimiter is wrong. It is very bad data. I assume you did not make that decision, if you did then I apologize but I assume you agree that they are doing it wrong.

显然有18个项目。您可以在所有逗号中拆分数据,如s.Split(',')。然后,如果有18个项目,那么您可以使用它们而无需特殊处理。如果超过18,则执行以下操作。取第一个n1项,无论
是该字段之前的项目数。相应地,取最后的n2个项目,无论该字段后面的数字是什么。然后你知道附加项目的数量是数组中的项目数减去18.以
开始的连接是项目开始位置的项目,并继续获取额外项目的数量。你不知道吗?如果您有任何帮助,请说出来。

Apparently there are 18 items. You can split the data at all commas, as in s.Split(','). Then if there are 18 items then you can use them without special processing. If there are more than 18 then do the following. Take the first n1 items, whatever are the number of items that precede the field. Correspondingly, take the last n2 items, whatever is the number that follows the field. Then you know that the number of additional items is the number of items in the array minus 18. Do a join starting with the item at the position that the item begins and continuing for the number of extra items. Do you understnand? If there is anything you ned help with then say so.


这篇关于C#在字段中使用额外的逗号解析文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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