从csv读取并添加值 [英] read and add values from csv

查看:105
本文介绍了从csv读取并添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件,我需要读取csv文件并从csv中添加数据.当我编译时给了我错误:
错误1名称``CreateValues''在当前上下文中不存在

错误2运算符``!=''不能应用于类型为``ReadCsvFile.DailyValues''和null
的操作数
错误3名称行"在当前上下文中不存在



I have a csv file and i need to read the csv file and add data from the csv. when i compile it gave me error:
Error 1 The name ''CreateValues'' does not exist in the current context

Error 2 Operator ''!='' cannot be applied to operands of type ''ReadCsvFile.DailyValues'' and null

Error 3 The name ''rows'' does not exist in the current context



 // reads stock values from csv file into a List
 public static List<DailyValues> GetStockValues(string filePath)
        {
            List<DailyValues> values = new List<DailyValues>();
          
            using (StreamReader reader = new StreamReader(filePath))
            {
                string line;
                DailyValues v;
                while ((line = reader.ReadLine()) != null)
                {
                    v = CreateValues(line);
                    if (v != null)
                        values.Add(v);
                }
            }
            return values;
// TODO: add the data from the csv file
            foreach (string row in rows)
            {
                if (string.IsNullOrEmpty(row)) continue;
                string[] cols = row.Split(',');
                DailyValues v = new DailyValues();

                v.Open = Convert.ToDecimal(cols[0]);
                v.High = Convert.ToDecimal(cols[1]);
                v.Low = Convert.ToDecimal(cols[2]);
                v.Close = Convert.ToDecimal (cols[3]);
                v.Volume = Convert.ToDecimal (cols[4]);
                v.AdjClose = Convert.ToDecimal(cols[5]);
                v.Date = Convert.ToDateTime(cols[6]);
                values.Add(v);


                return values;

推荐答案

>>错误1名称"CreateValues" '在当前上下文中不存在

CreateValues函数是否存在.如果是这样,它是否是静态的.如果不是,则将其设为静态或从GetStockValues中删除静态.

>>错误2运算符``!=''不能应用于类型为``ReadCsvFile.DailyValues''且为null的操作数

DailyValues是结构吗?您不能将空检查应用于struct.
更改以下内容
>>Error 1 The name ''CreateValues'' does not exist in the current context

Does CreateValues function exists. If it does, is it static. If not, either make it static or remove static from GetStockValues.

>>Error 2 Operator ''!='' cannot be applied to operands of type ''ReadCsvFile.DailyValues'' and null

Is DailyValues struct ? You cannot apply null check to struct.
Change following
if (v != null)




to

if (v != default(DailyValues))



>>错误3名称行"在当前上下文中不存在
行在哪里?它不见了.该错误是不言自明的.添加变量行(应该在数据类型上实现IEnumerable(某些列表/数组是简单的单词))



>>Error 3 The name ''rows'' does not exist in the current context
Where are rows ? It is missing. The error is self explanatory. Add a variable rows (which should have IEnumerable implemented on data type (some list/array is simple words))


使用此链接

http://stackoverflow.com/questions/7702573/importing-csv-data- into-c-sharp-classes [ ^ ]
Use this link

http://stackoverflow.com/questions/7702573/importing-csv-data-into-c-sharp-classes[^]


阅读带有C#的CSV文件
使用C#读取CSV文件
快速CSV阅读器
使用C#br mode ="hold"/>
读取CSV文件
试试上面的链接,您将获得解决方案:)
Read CSV file with C#
Read CSV file with C#
Fast CSV reader
Read CSV file with C#br mode="hold" />

Try the above links out , you''ll get the solution :)


这篇关于从csv读取并添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆