【问题标题】:Removing a comma within double quotes, then remove double quotes删除双引号内的逗号,然后删除双引号
【发布时间】:2016-05-05 02:13:37
【问题描述】:

我在解析 .csv 时使用 FileHelpers 创建分隔符字段时遇到问题。我的这一行数据在 LINQ 操作期间总是崩溃,类似于他们网站上的 filehelper 示例。

错误出现在列有引号的地方,例如这一行:

data1, data2, "data3a, data3b", data4

如何删除引号中的逗号,然后删除 data3a 和 data3b 的引号?

.csv 文件中的特定列有两个内容,但我不知道应该为此使用什么分隔符字段。

如果我不清楚,或者这个问题得到了回答,或者下面的评论没有目的。

非常感谢。

编辑: 类的代码

行:2010,NON-HISPANIC BLACK,MALE,"NEPHRITIS, NEPHROTIC SYNDROME AND NEPHROSIS",70,1

//delimiters, ignore first row, separate with comma
[DelimitedRecord(",")]
[IgnoreFirst()]
public class CauseofDeathClass
{

    public int Year { get; set; }
    public string Ethnicity { get; set; }
    public string Sex { get; set; }
    public string Cause_of_Death { get; set; }
    public int Count { get; set; }
    public int Percent { get; set; }

}

【问题讨论】:

标签: c# csv entity delimiter filehelpers


【解决方案1】:

使用FieldQuoted 属性。请参阅 here 了解不同的 QuoteMode 选项。

//delimiters, ignore first row, separate with comma
[DelimitedRecord(",")]
[IgnoreFirst()]
public class CauseofDeathClass
{    
    public int Year { get; set; }
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string Ethnicity { get; set; }
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string Sex { get; set; }
    [FieldQuoted('"', QuoteMode.OptionalForBoth)]
    public string Cause_of_Death { get; set; }
    public int Count { get; set; }
    public int Percent { get; set; }
}

【讨论】:

    猜你喜欢
    • 2018-12-15
    • 2019-08-11
    • 2018-10-24
    • 2016-03-18
    • 2018-07-27
    • 2020-01-19
    • 2019-08-11
    • 2015-07-26
    • 1970-01-01
    相关资源
    最近更新 更多