Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Csv文件中编辑一列_C#_Csv - Fatal编程技术网

C# 在Csv文件中编辑一列

C# 在Csv文件中编辑一列,c#,csv,C#,Csv,我试图在我的csv中只编辑一列。但是,代码似乎不会影响文件。我要做的更改是用逗号分隔第四列数据 class Program { static void Main(string[] args) { var filePath = Path.Combine(Directory.GetCurrentDirectory(), "kaviaReport 02_08_2016.csv"); var fileContents = ReadFile(filePath

我试图在我的csv中只编辑一列。但是,代码似乎不会影响文件。我要做的更改是用逗号分隔第四列数据

class Program
{
    static void Main(string[] args)
    {
        var filePath = Path.Combine(Directory.GetCurrentDirectory(), "kaviaReport 02_08_2016.csv");
        var fileContents = ReadFile(filePath);
        foreach (var line in fileContents)
        {
            Console.WriteLine(line);
        }

        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }

    public static IList<string> ReadFile(string fileName)
    {
        var results = new List<string>();
        int lineCounter = 0;
        string currentLine = string.Empty;
        var target = File
 .ReadAllLines(fileName);
        while ((currentLine = fileName) != null)//while there are lines to read
        {
            List<string> fielded = new List<string>(currentLine.Split(','));
            if (lineCounter != 0)
            {
                //If it's not the first line
                var lineElements = currentLine.Split(',');//split your fields into an array
                var replace = target[4].Replace(' ', ',');//replace the space in position 4(field 5) of your array
                results.Add(replace);
                //target.WriteAllLines(string.Join(",", fielded));//write the line in the new file

            }

            lineCounter++;
            File.WriteAllLines(fileName, target);


        }

        return results;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
var filePath=Path.Combine(Directory.GetCurrentDirectory(),“kaviaReport 02_08_2016.csv”);
var fileContents=ReadFile(filePath);
foreach(fileContents中的var行)
{
控制台写入线(行);
}
Console.WriteLine(“按任意键退出…”);
Console.ReadKey();
}
公共静态IList读取文件(字符串文件名)
{
var results=新列表();
int lineCounter=0;
字符串currentLine=string.Empty;
var target=File
.ReadAllLines(文件名);
while((currentLine=fileName)!=null)//当有行要读取时
{
List fielded=新列表(currentLine.Split(',');
如果(行计数器!=0)
{
//如果不是第一行的话
var lineElements=currentLine.Split(',');//将字段拆分为数组
var replace=target[4]。replace(“”,“);//替换数组位置4(字段5)中的空格
结果:添加(替换);
//target.writeAllines(string.Join(“,”,fielded));//在新文件中写入该行
}
lineCounter++;
writeAllines(文件名,目标);
}
返回结果;
}
}

当前代码有一些错误。
最大的一个是将
currentLine
分配给
fileName
。当然,如果你想在线路上循环,这是没有意义的。因此,您需要在阅读行上使用foreach。
然后在循环内部,您应该使用变量lineElements在拆分
currentLine
后获得可用的5列
最后,文件的重写超出了循环,应该使用结果列表

// Loop, but skip the first line....
foreach(string currentLine in target.Skip(1))
{
    // split your line into an array of strings
    var lineElements = currentLine.Split(',');

    // Replace spaces with commas on the fifth column of lineElements 
    var replace = lineElements[4].Replace(' ', ',');

    // Add the changed line to the result list 
    results.Add(replace);
}

// move  outside the foreach loop the write of your changes 
File.WriteAllLines(fileName, results.ToArray());
在写这段代码时,我脑子里发生了一些事情。不清楚您是想用逗号展开的第五列中的数据重写CSV文件,还是想重写整行(也包括第0、1、2、3、4列等)。在后一种情况下,您需要不同的代码

// Replace spaces with commas on the fifth column of lineElements 
// And resssign the result to the same fifth column
lineElements[4] = lineElements[4].Replace(' ', ',');

// Add the changed line to the result list putting the comma 
// between the array of strings lineElements
results.Add(string.Join(",", lineElements);
while((currentLine=fileName)!=null)
将设置currentLine=fileName,这将使该行始终为真并形成一个无限循环

我会把它写成for循环,而不是一段时间

public static IList<string> ReadFile(string fileName)
    {
        var target = File.ReadAllLines(fileName).ToList();
        // i = 1 (skip first line)
        for (int i = 1; i < target.Count; i++)
        {
           target[4] = target[4].Replace(' ', ','); //replace the space in position 4(field 5)
        }
        File.WriteAllLines(fileName, target);
        // Uncomment the RemoveAt(0) to remove first line
        // target.RemoveAt(0);
        return target;
    }
公共静态IList读取文件(字符串文件名)
{
var target=File.ReadAllLines(fileName.ToList();
//i=1(跳过第一行)
for(int i=1;i
而((currentLine=fileName)!=null)
。。。想解释一下吗?如果您没有任何限制,请尝试library:CSVHelper()。此任务变得不那么琐碎。感谢您的帮助,它似乎正在做我想做的事情。但是,它只影响每列中的第5行,而不仅仅是第5列。非常感谢您的解决方案,这解决了我的问题。但是,当我检查更新的文件时,它只显示已编辑的第5列。我想查看所有的专栏+更新的专栏是的,我想知道。如果你想重写整行代码,你需要代码的第二部分。如果不清楚的话,我可以换掉现在的一个。非常感谢你,一切正常。非常感谢