Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
如何切换csv文件的列(c#)_C# - Fatal编程技术网

如何切换csv文件的列(c#)

如何切换csv文件的列(c#),c#,C#,我有一个5列的CSV文件,我需要把第三列放在前面 我该怎么做? 下面是我认为我必须做的 //Basically I have to make a fore loop which results in a multidimensional array //Then write another for loop switching the colums of the array around //Then sorting it //then

我有一个5列的CSV文件,我需要把第三列放在前面

我该怎么做? 下面是我认为我必须做的

     //Basically I have to make a fore loop which results in a multidimensional array
        //Then write another for loop switching the colums of the array around
        //Then sorting it
        //then saving it
更新:csv文件如下所示

字符串,字符串,双精度,双精度,字符串

例如

约翰,史密斯,.464,10.5,单词等等


还应该提到这个过程需要可逆,这意味着移动的列需要放回原来的位置。

最初我在评论中发布了我的解决方案,但我认为它应该得到一个正确的答案

我有一个5列的CSV文件,我需要把第三列放在前面。我该怎么做

  • 使用您喜爱的电子表格程序打开该文件
  • 向左拖动第三列
  • (根据需要创建排序)
  • 拯救
  • 工作完成
  • 现在一些读者可能会争辩说,这个问题加了
    C
    ,因此我的答案是无效的。我认为OP从来没有提供任何证据证明这样的任务必须通过编程语言来解决(思考),从而使可接受的答案像OP的问题一样模糊(电子表格程序有资格获得这样的答案)


    @Gooyan可以随时更新您的问题,详细说明您需要什么(以及为什么),您已经尝试了什么(如果有的话),我很乐意相应地更新我的答案。

    一个简单的读写循环应该可以完成这项工作,而无需将所有数据加载到内存中

    注意:下面的解决方案假设一个简单的CSV文件,其中字段不包含分隔符(“,”),并且每行包含相同数量的列

        /// <summary>
        /// Move a csv file column to a new position. File is modified in place.
        /// </summary>
        public void MoveCsvColumn(string file, int column, int position, string delimeter = ",")
        {
            using (var reader = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Write)))
            using (var writer = new StreamWriter(new FileStream(file, FileMode.Open, FileAccess.Write, FileShare.Read)))
            {
                while (!reader.EndOfStream)
                {
                    // Read the line
                    var line = reader.ReadLine().Split(new string[] { delimeter }, StringSplitOptions.None);
    
                    // Move the column within the array of columns
                    MoveColumn(line, column, position);
    
                    // write the output
                    writer.WriteLine(string.Join(delimeter, line));
                }
            }
        }
    
        /// <summary>
        /// Move a column within the array to the new destination
        /// </summary>
        public void MoveColumn(object[] line, int from, int to)
        {
            to = Math.Max(0, Math.Min(line.Length - 1, to));
    
            if (from == to 
                || from < 0 
                || from >= line.Length)
            {
                return;
            }
    
            while (from != to)
            {
                if (from > to)
                {
                    // percolate down
                    Swap(line, from, from-1);
                    from--;
                }
                else
                {
                    // percolate up
                    Swap(line, from, from + 1);
                    from++;
                }
            }
        }
    
        /// <summary>
        /// Swap values positions within the array
        /// </summary>
        public void Swap(object[] line, int a, int b)
        {
            var tmp = line[a];
            line[a] = line[b];
            line[b] = tmp;
        }
    
    //
    ///将csv文件列移动到新位置。文件已修改到位。
    /// 
    public void MoveCsvColumn(字符串文件,int列,int位置,字符串delimeter=“,”)
    {
    使用(var reader=newstreamreader(newfilestream(file,FileMode.Open,FileAccess.Read,FileShare.Write)))
    使用(var writer=newstreamwriter(newfilestream(file,FileMode.Open,FileAccess.Write,FileShare.Read)))
    {
    而(!reader.EndOfStream)
    {
    //读台词
    var line=reader.ReadLine().Split(新字符串[]{delimeter},StringSplitOptions.None);
    //在列数组中移动列
    移动列(行、列、位置);
    //写入输出
    writer.WriteLine(string.Join(delimeter,line));
    }
    }
    }
    /// 
    ///将数组中的列移动到新目标
    /// 
    public void MoveColumn(对象[]行,int-from,int-to)
    {
    to=Math.Max(0,Math.Min(line.Length-1,to));
    如果(从==到)
    ||从<0
    ||from>=行长度)
    {
    返回;
    }
    while(从!=到)
    {
    如果(从>到)
    {
    //渗透
    交换(行、从、从-1);
    从--;
    }
    其他的
    {
    //渗透
    交换(行、从、从+1);
    来自++;
    }
    }
    }
    /// 
    ///交换数组中的值和位置
    /// 
    公共无效交换(对象[]行,int a,int b)
    {
    var tmp=第[a]行;
    第[a]行=第[b]行;
    第[b]行=tmp;
    }
    
    我很确定你可以用C#来完成这项任务,如果你选择接受它,这是你的使命,它应该是相当直接的,但我感觉如果文件太大,这将是一项资源密集型任务@AD8是的,文件越大,代码越重。但是,如果您首先没有按照正确的顺序制作CSV(如果此任务是生产任务),那么这种惩罚是值得的。@Gooyan,您可以添加一些需要更新的文件记录吗?您需要(1)将CSV加载为记录列表,(2)转换字段,(3)将记录另存为新的CSV。这就是全部。