阅读CSV对象的列表 [英] Read CSV to list of objects
问题描述
我有一个CSV具有不同的数据(日期时间,十进制)的列表文件。从CSV样本行:
I have a CSV file with a listing of varied data(datetime, decimal). Sample line from CSV:
Date,Open,High,Low,Close,Volume,Adj Close //I need to skip this first line as well
2012-11-01,77.60,78.12,77.37,78.05,186200,78.05
我创建,我想每一行读入对象列表。为对象的构造器是如下,分别从每个CSV行的字段被用来在这里分配。
I have a list of objects created that I want to read each of the lines into. The constructor for the objects is below, each of the fields from each CSV line is used and assigned here.
public DailyValues(DateTime date, decimal open, decimal high, decimal low,
decimal close, decimal volume, decimal adjClose)
: this()
{
Date = date;
Open = open;
High = high;
Low = low;
Close = close;
Volume = volume;
AdjClose = adjClose;
}
List<DailyValues> values = new List<DailyValues>();
有没有一种简单的方法来读取CSV的每一行进入我的列表值
并适当地分配给每个属性(即日期,开盘价,最高价)?
Is there an easy way to read each line of the CSV into my list values
and appropriately assign each attribute (i.e. date, open, high)?
推荐答案
为什么不干脆解析这些明确?你有属性的数量有限,所以它不是很困难的。而不是使用一个构造函数,需要很多争论,我用它返回一个新的 DailyValues
实例作为它的返回类型的静态方法。这类似于 DateTime.FromBinary
等
Why not just parse these explicitly? You have a limited number of properties, so it's not very difficult. Instead of using a constructor requiring many arguments, I used a static method that returns a new DailyValues
instance as it's return type. This is similar to DateTime.FromBinary
etc.
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace CsvDemo
{
class Program
{
static void Main(string[] args)
{
List<DailyValues> values = File.ReadAllLines("C:\\Users\\Josh\\Sample.csv")
.Skip(1)
.Select(v => DailyValues.FromCsv(v))
.ToList();
}
}
class DailyValues
{
DateTime Date;
decimal Open;
decimal High;
decimal Low;
decimal Close;
decimal Volume;
decimal AdjClose;
public static DailyValues FromCsv(string csvLine)
{
string[] values = csvLine.Split(',');
DailyValues dailyValues = new DailyValues();
dailyValues.Date = Convert.ToDateTime(values[0]);
dailyValues.Open = Convert.ToDecimal(values[1]);
dailyValues.High = Convert.ToDecimal(values[2]);
dailyValues.Low = Convert.ToDecimal(values[3]);
dailyValues.Close = Convert.ToDecimal(values[4]);
dailyValues.Volume = Convert.ToDecimal(values[5]);
dailyValues.AdjClose = Convert.ToDecimal(values[6]);
return dailyValues;
}
}
}
当然,你仍然可以添加一个默认的构造函数,你会希望添加异常处理解析失败的情况下(你也可以使用的TryParse
为)。
- 的
File.ReadAllLines
读取来自CSV所有行文件转换成字符串数组。 - 的
.Skip(1)
跳过标题行。 - 的
。选择(v = > DailyValues.FromCsv(v))
使用LINQ to选择每一行,并创建一个新的DailyValues
实例使用FromCsv
方法。这将创建一个System.Collections.Generic.IEnumerable< CsvDemo.DailyValues方式>
键入 - 最后,
.ToList()
convers的的IEnumerable
到列表
来匹配你想要的类型。
- The
File.ReadAllLines
reads all lines from the CSV file into a string array. - The
.Skip(1)
skips the header line. - The
.Select(v => DailyValues.FromCsv(v))
uses Linq to select each line and create a newDailyValues
instance using theFromCsv
method. This creates aSystem.Collections.Generic.IEnumerable<CsvDemo.DailyValues>
type. - Finally, the
.ToList()
convers theIEnumerable
to aList
to match the type you want.
而不是使用LINQ的,你可以简单地使用的foreach
循环来补充每个 DailyValues
实例到列表中。
Instead of using Linq you could have simply used a foreach
loop to add each DailyValues
instance to your list.
这篇关于阅读CSV对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!