TinyCsvParser不返回任何结果

时间:2018-12-20 17:10:40

标签: c# .net csv asp.net-core .net-core

我目前正在尝试使用for item in a: d = {} d["AppName"]= item["name"] d["AppId"] = item["id"] d["Health"] = item.get("health", "Null") d["place1"] = item.get("cities", {}).get("place1", "0") d["place2"] = item.get("cities", {}).get("place2", "0") 来解析文件,尽管我的测试内容有很多条目,但仍返回0个结果。代码如下:

映射类:

TinyCsvParser

我正在尝试从CSV行创建其列表的模型类:

public class TransactionCsvMapping : CsvMapping<TransactionDto>
{
    public TransactionCsvMapping()
    : base()
    {
        MapProperty(0, x => x.OccurenceDate);
        MapProperty(1, x => x.Name);
        MapProperty(4, x => x.Amount);
    }
}

解析CSV文件:

public class TransactionDto
{
    public string Name { get; set; }

    public double Amount { get; set; }

    public DateTime OccurenceDate { get; set; }
}

这里是解析时CsvParserOptions csvParserOptions = new CsvParserOptions(true, ','); CsvReaderOptions csvReaderOptions = new CsvReaderOptions(new[] { Environment.NewLine }); TransactionCsvMapping csvMapper = new TransactionCsvMapping(); CsvParser<TransactionDto> csvParser = new CsvParser<TransactionDto>(csvParserOptions, csvMapper); var result = csvParser .ReadFromString(csvReaderOptions, fileContents) .ToList(); //results is empty here! 的内容:

fileContents

文字字符串为:

"Date","Transaction","Name","Memo","Amount"
"9/7/2016","DEBIT","DEBIT PURCHASE","Food Yummy","-4.8000"
"9/7/2016","DEBIT","DEBIT PURCHASE","Gas Blah","-28.0000"

为什么我在这里没有得到任何结果?我该如何克服?

1 个答案:

答案 0 :(得分:2)

我通过使用您的代码成功获得了结果。

enter image description here

使用\r\n代替\n作为换行符,或使用\n声明换行符

new CsvReaderOptions(new[] { "\n" });