如何复制包含列标题的csv文件的全部内容,并用不同的字母替换每个列值 [英] How to copy the entire contents of the csv file including column headers and replace each column values with different alphabets

查看:65
本文介绍了如何复制包含列标题的csv文件的全部内容,并用不同的字母替换每个列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以显示值,但无法读取列标题,然后用字母替换列中的所有1



我的csv看起来像这样:

I am able to display the values but unable to read the column headers and then replace all 1's in a column with a an alphabet

my csv look's like this:

c1 c2 c3
0  1  0
1  0  1
1  0  0
0  1  0
1  0  0   





现在我的数组只有



now my array has only

0  1  0
1  0  1
1  0  0
0  1  0
1  0  0  



没有列标题和我正在尝试将我的输出显示为


without the column headers and i am trying to display my ouput as

c1 c2 c3
0  b  0
a  0  c
a  0  0
0  b  0
a  0  0





我的尝试:



这是我的代码:



What I have tried:

here is my code:

List<string> lines = new List<string>();
 using (StreamReader r = new StreamReader(File.OpenRead(@"d/as.csv")))
            {
               
                string line;
                while ((line = r.ReadLine()) != null)
                {
                   lines.Add(line);
                }
            }
            string[] Sensor_Array = lines.ToArray();
            
           
            foreach (var s in Sensor_Array)
            {
                Console.WriteLine(s.ToString());
            }
            Console.ReadLine();

推荐答案

var lines = File.ReadAllLines(@"d:\as.csv");
for (var i = 1; i < lines.Length; i++)
{
  var lineSplit = lines[i].Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  lineSplit[0] = lineSplit[0].Equals("1") ? "a" : lineSplit[0];
  lineSplit[1] = lineSplit[1].Equals("1") ? "b" : lineSplit[1];
  lineSplit[2] = lineSplit[2].Equals("1") ? "c" : lineSplit[2];
}


这篇关于如何复制包含列标题的csv文件的全部内容,并用不同的字母替换每个列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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