Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析分号delimeter文件_C#_Regex_Csv_Textfieldparser - Fatal编程技术网

C# 解析分号delimeter文件

C# 解析分号delimeter文件,c#,regex,csv,textfieldparser,C#,Regex,Csv,Textfieldparser,我有一个CSV文件,但分隔符是分号并且每列都用双引号括起来。还出现了

我有一个CSV文件,但分隔符是分号
并且每列都用双引号括起来。还出现了
&

我正在使用TextFieldParser来解析文件。以下是示例数据:

“A001”;“RT:这是一条推特”;"http://www.whatever.com/test/module &;一“

对于上面的例子,我得到的列/字段比我应该得到的要多

字段[0]=“A001”

字段[1]=“RT:这是一条推文”

字段[2]=”http://www.whatever.com/test/module&“

字段[3]=“一”

这是我的密码。需要做哪些更改来处理这种情况

 using (var parser  =  new TextFieldParser(fileName))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(";");
                parser.TrimWhiteSpace = true;
                parser.HasFieldsEnclosedInQuotes = false;

                int rowIndex = 0;
                PropertyInfo[] properties = typeof(TwitterData).GetProperties();
                while (parser.PeekChars(1) != null)
                {
                    var cleanFieldRowCells = parser.ReadFields().Select(
                        f => f.Trim(new[] { ' ', '"' }));

                    var twitter = new TwitterData();
                    int index = 0;
                    foreach (string c in cleanFieldRowCells)
                    {
                            string str = c;

                            if (properties[index].PropertyType == typeof(DateTime))
                            {
                                string twitterDateTemplate = "ddd MMM dd HH:mm:ss +ffff yyyy";
                                DateTime createdAt = DateTime.ParseExact(str, twitterDateTemplate, new System.Globalization.CultureInfo("en-AU"));
                                properties[index].SetValue(twitter, createdAt);
                            }
                            else
                            {
                                properties[index].SetValue(twitter, str);
                            }

                        index++;
                    }
                }

-Alan-

使用上面的两个示例字符串,并将
HasFieldsEnclosedInQuotes
属性设置为true,这对我来说非常有用

string LINES = @"
    ""A001"";""RT:This is a tweet""; ""http://www.whatever.com/test/module&one""
    ""A001"";""RT: Test1 ; Test2"";""test.com"";   
";
using (var sr = new StringReader(LINES))
{
    using (var parser = new TextFieldParser(sr))
    {
        parser.TextFieldType = FieldType.Delimited;
        parser.SetDelimiters(";");
        parser.TrimWhiteSpace = true;
        parser.HasFieldsEnclosedInQuotes = true;

        while (parser.PeekChars(1) != null)
        {
            var cleanFieldRowCells = parser.ReadFields().Select(
                f => f.Trim(new[] { ' ', '"' })).ToArray();
            Console.WriteLine("New Line");
            for (int i = 0; i < cleanFieldRowCells.Length; ++i)
            {
                Console.WriteLine(
                    "Field[{0}] = [{1}]", i, cleanFieldRowCells[i]
                );
            }
            Console.WriteLine("{0}", new string('=', 40));
        }
    }
}

您是否尝试将
HasFieldsEnclosedInQuotes
设置为true?是的,但在每一行上尝试调用System.Net.WebUtility.HtmlDecode()。它将转动
&编码为“&”,以及解码任何其他内容。我确实有一个场景,其中数据看起来像“A001”;“RT:Test1;Test2”;"". 上面的代码是否也处理“Test1;Test2”
System.Net.WebUtility.HtmlDecode()
只解码
HTML
。即
&
,等等。它不会影响其他任何东西,所以类似于
“A001”;“RT:Test1;Test2”;“test.com”将被忽略。上述操作可以正常工作。但是,如果我将行复制到文件并从文件(file.ReadAllText)中读取内容,我会遇到异常(“无法使用当前分隔符解析第1行”)@AlanB-您不能按原样复制
行,因为它是
C#逐字字符串
。即,双引号被转义。而是使用文本文件,然后可以像在原始示例中那样传递文本文件的路径,而不是使用
file.ReadAllText()
New Line
Field[0] = [A001]
Field[1] = [RT:This is a tweet]
Field[2] = [http://www.whatever.com/test/module&amp;one]
========================================
New Line
Field[0] = [A001]
Field[1] = [RT: Test1 ; Test2]
Field[2] = [test.com]
Field[3] = []
========================================