c# - 删除文件助手中的引号

标签 c# filehelpers

我有一个.csv文件(我无法控制数据),由于某种原因,它的所有内容都用引号引起来。

"Date","Description","Original Description","Amount","Type","Category","Name","Labels","Notes"
"2/02/2012","ac","ac","515.00","a","b","","javascript://"
"2/02/2012","test","test","40.00","a","d","c",""," "


我正在使用filehelpers,我想知道删除所有这些引号的最佳方法是什么?是否有句话说“如果我删除引号。如果找不到引号,则什么都不做”?

这会弄乱数据,因为我将在"\"515.00\""处加上不必要的多余引号(特别是因为在这种情况下,我希望它是一个小数而不是一个字符串”。

我也不确定“ javascript”的全部含义以及为什么生成它,但这来自我无法控制的服务。

编辑
这就是我使用csv文件的方式。

    using (TextReader textReader = new StreamReader(stream))
        {
            engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; 

            object[] transactions = engine.ReadStream(textReader);
        }

最佳答案

您可以使用在属性页面here上描述得最好的FieldQuoted属性。请注意,该属性可以应用于任何FileHelpers字段(即使它键入Decimal)。 (请记住,FileHelpers类描述了导入文件的规范。因此,当您将Decimal字段标记为FieldQuoted时,就是在文件中说,该字段会被引用。)

您甚至可以指定引号是否为可选

[FieldQuoted('"', QuoteMode.OptionalForBoth)] 


这是一个可以处理您的数据的控制台应用程序:

class Program
{
    [DelimitedRecord(",")]
    [IgnoreFirst(1)]
    public class Format1
    {
        [FieldQuoted]
        [FieldConverter(ConverterKind.Date, "d/M/yyyy")]
        public DateTime Date;
        [FieldQuoted]
        public string Description;
        [FieldQuoted]
        public string OriginalDescription;
        [FieldQuoted]
        public Decimal Amount;
        [FieldQuoted]
        public string Type;
        [FieldQuoted]
        public string Category;
        [FieldQuoted]
        public string Name;
        [FieldQuoted]
        public string Labels;
        [FieldQuoted]
        [FieldOptional]
        public string Notes;
    }

    static void Main(string[] args)
    {
        var engine = new FileHelperEngine(typeof(Format1));

        // read in the data   
        object[] importedObjects = engine.ReadString(@"""Date"",""Description"",""Original Description"",""Amount"",""Type"",""Category"",""Name"",""Labels"",""Notes""
""2/02/2012"",""ac"",""ac"",""515.00"",""a"",""b"","""",""javascript://""
""2/02/2012"",""test"",""test"",""40.00"",""a"",""d"",""c"","""","" """);

        // check that 2 records were imported
        Assert.AreEqual(2, importedObjects.Length);

        // check the values for the first record
        Format1 customer1 = (Format1)importedObjects[0];
        Assert.AreEqual(DateTime.Parse("2/02/2012"), customer1.Date);
        Assert.AreEqual("ac", customer1.Description);
        Assert.AreEqual("ac", customer1.OriginalDescription);
        Assert.AreEqual(515.00, customer1.Amount);
        Assert.AreEqual("a", customer1.Type);
        Assert.AreEqual("b", customer1.Category);
        Assert.AreEqual("", customer1.Name);
        Assert.AreEqual("javascript://", customer1.Labels);
        Assert.AreEqual("", customer1.Notes);

        // check the values for the second record
        Format1 customer2 = (Format1)importedObjects[1];
        Assert.AreEqual(DateTime.Parse("2/02/2012"), customer2.Date);
        Assert.AreEqual("test", customer2.Description);
        Assert.AreEqual("test", customer2.OriginalDescription);
        Assert.AreEqual(40.00, customer2.Amount);
        Assert.AreEqual("a", customer2.Type);
        Assert.AreEqual("d", customer2.Category);
        Assert.AreEqual("c", customer2.Name);
        Assert.AreEqual("", customer2.Labels);
        Assert.AreEqual(" ", customer2.Notes);
    }
}


(请注意,您的第一行数据似乎有8个字段,而不是9个字段,因此我用Notes标记了FieldOptional字段)。

关于c# - 删除文件助手中的引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9133867/

相关文章:

C#.NET : Read CSV using % converter

c# - 如何使用 FileHelpers 编写带标题的 CSV 文件?

c# - 写入 CSV 文件

c# - 如何视觉上连接2个圈子?

c# - FileHelpers在调用engine.ReadNext()方法和readign engine.LineNumber属性之间的线程锁定问题

c# - 自动续订 Azure 身份验证的最佳方法是什么?

c# - 选择异步函数的更好方法?

c# - 文件助手源代码

c# - 多选枚举

c# - 将List <List <string >>转换为List <string>