【发布时间】:2014-05-27 00:30:26
【问题描述】:
我正在尝试来自http://www.filehelpers.net/ 的一些非常简单的示例,只要我使用它们就可以工作;或,作为分隔符。如果我尝试按制表符分隔它会引发异常。
The delimiter '\t' can´t be found after the field 'CustomterId' at line 2 (the record has less fields, the delimiter is wrong or the next field must be marked as optional).
C#类
[IgnoreEmptyLines(true), IgnoreFirst(1), DelimitedRecord(@"\t"), IgnoreFirst(0)]
public class Customer
{
// Fields
public bool Active;
public int CustomerId;
[FieldConverter(ConverterKind.Date, "ddMMyyyy")]
public DateTime CustomerJoinded;
[FieldTrim(TrimMode.Both), FieldQuoted('"')]
public string CustomerName;
}
要读取的文件:
CustomterId CusomterName CustomerJoinded Active
1001 ABCD EDF 05052012 TRUE
1002 qwetqwt 15052013 FALSE
1003 wtqwtqwt 03052013 TRUE
1004 qwetwtqwet 04052013 FALSE
1005 gwrtgqwtqwt 05052013 TRUE
1006 rgherghrtqw4gz 07052013 FALSE
1007 wgwherhreh 08052013 TRUE
1008 sfagh34hq3h 09052013 FALSE
1009 wf2rgahasrg 12052013 TRUE
1010 sfgwe3g 05052013 FALSE
1011 fsfwrvg 05052013 TRUE
1012 ra34qbdfb 05052013 FALSE
1013 wtqwg 05052013 TRUE
1014 dsgag3 05052013 FALSE
1015 hgrh 05052013 TRUE
1016 werw 05052013 FALSE
你能告诉我这里有什么问题吗?
【问题讨论】:
-
您的示例用空格分隔。这是由于粘贴到网站,还是实际上是字段之间的空格?如果是这样,这就解释了为什么解析器期望
\t但找到了` `。 -
虽然它显示为空格,但实际上是选项卡
-
再想一想...我在您的
DelimitedRecord属性中看到您已经编写了@"\t",但该语法适用于逐字字符串文字,例如,它等同于"\\t"。尝试杀死@以使其解析实际的制表符。 -
@FactorMystic 感谢良好的起点。
-
@FactorMystic 请给出答案以接受它。有用。非常感谢。
标签: c#-4.0 filehelpers csv