简体   繁体   English

将字符串csv转换为对象列表

[英]convert string csv to List of objects

I have the following sting 我有以下刺痛

const string csv = "Foo1,Foo2,Foo3,Foo4,Foo5,Foo6,Ping Pong\n" +
                   "2016-02-29,1437.530029,1445.839966,1433.77002,1436.930054,34016300,1436.930054\n" +
                   "2016-02-25,1431.439941,1431.439941,1421.280029,1426.97998,78076500,1426.97998\n" +
                   "2016-02-24,1430.459961,1432.430054,1417.449951,1419.790039,29049900,1419.790039\n";

How I can convert it to List<Model> where 我如何将其转换为List<Model>在哪里

public class Model
{
    public string Foo1 { get; set; }
    public string Foo2 { get; set; }
    public string Foo3 { get; set; }
    public string Foo4 { get; set; }
    public string Foo5 { get; set; }
    public string Foo6 { get; set; }
    public string Ping_Pong { get; set; }
}

Note of the Ping Pong header in my orig csv 我的orig csv中的Ping Pong标题的注释

I tried to use CsvHelper but with no success as it is taking a stream rather then string and I failed trying to convert a parse 我试图使用CsvHelper,但没有成功,因为它采取流而不是字符串,我没有尝试转换解析

EDIT It does not matter for me if to use CsvHelper or not, in the end I want to convert the csv to List<Model> 编辑对我来说无论是否使用CsvHelper并不重要,最后我想将csv转换为List<Model>

How can I do this? 我怎样才能做到这一点?

You can create an StringReader using csv variable: 您可以使用csv变量创建StringReader

var textReader = new StringReader(csv);

var csvr = new CsvReader(textReader);
var records = csvr.GetRecords<Model>();

If you want your own parser: 如果你想要自己的解析器:

var lines = csv.Split(new char[] {'\n'}, StringSplitOptions.RemoveEmptyEntries).Skip(1);
List<Model> models = new List<Model>();

foreach (var item in lines)
{
    var values = item.Split(',');
    var model = new Model
    {
        Foo1 = values[0],
        Foo2 = values[1],
        Foo3 = values[2],
        Foo4 = values[3],
        Foo5 = values[4],
        Foo6 = values[5],
        Ping_Pong = values[6],
    };

    models.Add(model);
}

EDIT: 编辑:

To resolve the header problem using CsvHelper you need create a map configuration class specifying the mappings between headers and properties: 要使用CsvHelper解决标头问题,您需要创建一个映射配置类,指定标头和属性之间的映射:

public class ModelMap : CsvClassMap<Model>
{
    public ModelMap()
    {
        Map(m => m.Foo1);
        Map(m => m.Foo2);
        Map(m => m.Foo3);
        Map(m => m.Foo4);
        Map(m => m.Foo5);
        Map(m => m.Foo6);
        Map(m => m.Ping_Pong).Name("Ping Pong");
    }
}

Using like this: 使用这样:

var textReader = new StringReader(csv);

var csvr = new CsvReader(textReader);
csvr.Configuration.RegisterClassMap<ModelMap>();

var records = csvr.GetRecords<Model>();

The problem is that your data set (the csv string) is missing a column (you specify 7 columns, but the Ping Pong column is missing from the csv). 问题是您的数据集(csv字符串)缺少一列(您指定了7列,但csv中缺少Ping Pong列)。 The default behavior will be to throw, but you can set a config option to ignore missing columns: 默认行为是throw,但您可以设置config选项以忽略缺少的列:

Here's working code: 这是工作代码:

var config = new CsvConfiguration();
// setting this will cause the missing Ping Pong field to be ignored
config.WillThrowOnMissingField = false;

// we wrap your string in a StringReader to make it accessible to CsvReader
var reader = new CsvReader(new StringReader(csv), config);
var records = reader.GetRecords<Model>().ToList();
records.Dump();

To do exactly what you asked you could use this: 要完全按照你的要求你可以使用这个:

var modelStrings = csv.Split('\n').Skip(1);
var models = new List<Model>();
foreach(string s in modelStrings)
{
    var props = s.Split(',');
    models.Add(new Model(props[0],props[1],props[2],props[3],props[4],props[5],props[6]));

}

but a word of warning: this is probably slow, and you might need to add more extensive logic for instantiating new models to account for possible changes in csv format. 但请注意:这可能很慢,您可能需要添加更广泛的逻辑来实例化新模型以解决csv格式的可能变化。

Edit: 编辑:

To clarify what this does, it first splits the csv string by new lines skipping the first one. 为了澄清这一点,它首先将跳过第一个的新行拆分csv字符串。 Then it uses each string in that list and splits them by commas to get a list of values (model properties) to instantiate the Model class with 然后它使用该列表中的每个字符串并用逗号分隔它们以获取值列表(模型属性)以实例化Model类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM