如何从逗号分隔的文件中读取值? [英] How to read values from a comma separated file?

查看:44
本文介绍了如何从逗号分隔的文件中读取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用c锐笔读取文本文件中用逗号分隔的行中的单词.

I want to read words in a text file of a line separated by commas in c sharp.

例如,我想读这一行:

9/10/2011 10:05,995.4,998.8,995.4,997.5,118000

并获取值: 9/10/2011 10:05 995.4 998.8 995.4 997.5 118000 .

and get the values: 9/10/2011 10:05, 995.4, 998.8, 995.4, 997.5 and 118000.

接下来,我还需要将日期格式更改为 MMddYYYY ,并将时间格式更改为 HHmmss (例如 100500 ).

Next, I also need to change the format of the date to MMddYYYY, and of the time to HHmmss (e.g. 100500).

我正在使用这段代码进行阅读,有什么问题

I am using this code for reading is there anything wrong

 private void button1_Click(object sender, EventArgs e)
 {
     StreamReader reader1 = File.OpenText(Path1);
     string str = reader1.ReadToEnd();
     reader1.Close();
     reader1.Dispose();
     //     File.Delete(Path1);

     string[] Strarray = str.Split(new char[] { Strings.ChrW(7) });
     int abc = Strarray.Length - 1;
     int xyz = 0;
     bool status = true;

     while (xyz <= abc)
     {
        try
        {
           status = true;
           string[] strarray1 = Strarray[xyz].Split(",".ToCharArray());
           string SecName = strarray1[0];
           int a2 = 0;
           while (status)  //If the selected list is empty or the text file has selected name this will execute
           {
              status = false;
              string SecSym = strarray1[1];
              int DT = int.Parse(strarray1[2]);
              int TM = int.Parse(strarray1[3]);
              float O = float.Parse(strarray1[2]);
              float H = float.Parse(strarray1[3]);
              float L = float.Parse(strarray1[4]);
              float C = float.Parse(strarray1[5]);
              double OI = double.Parse(Convert.ToString(0));
              float V = float.Parse(strarray1[6]);

              //   string a = string.Concat(SecName, ",",SecSym,",", DT, ",", TM, ",", O, ",", H, ",", L);
              //writer.WriteLine(a);
           }
        }
        catch
        { }
     }
   }
}

推荐答案

对于任务的第一部分,请使用以为分隔符的 Split 方法.要将字符串日期时间从一种格式转换为另一种格式,您需要将该字符串转换为日期时间( DateTime.Parse DateTime.ParseExact ),然后使用转换为最终格式DateTime.ToString 方法.

For 1st part of your task use Split method with , as separator. To convert string datetime from one format to another you need to convert that string to datetime(DateTime.Parse, DateTime.ParseExact) and then convert to final format using DateTime.ToString method.

这篇关于如何从逗号分隔的文件中读取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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