Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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#_List_Csv - Fatal编程技术网

C# 重新排序/添加空列时复制CSV文件

C# 重新排序/添加空列时复制CSV文件,c#,list,csv,C#,List,Csv,在重新排序/添加空列时复制CSV文件 例如,如果传入文件的任何一行的10列中有3列的值的顺序与输出的顺序不同,如(第一行除外,它是带有列名的标题): 而产量预计将有所增加 col0,col1,col2,col3,col4,col5,col6,col7,col8,col9 然后,对于输入文件中的col0、col1、col3、col5、col7、col8、col9以及col2、col4、col4中的值,输出应为“”。因此,对于显示的第二行(2,5,8),预期输出为“,,2,,,5,,,,,,” 下

在重新排序/添加空列时复制CSV文件

例如,如果传入文件的任何一行的10列中有3列的值的顺序与输出的顺序不同,如(第一行除外,它是带有列名的标题):

而产量预计将有所增加

col0,col1,col2,col3,col4,col5,col6,col7,col8,col9
然后,对于输入文件中的col0、col1、col3、col5、col7、col8、col9以及col2、col4、col4中的值,输出应为“”。因此,对于显示的第二行(2,5,8),预期输出为“,,2,,,5,,,,,,”

下面的代码我已经尝试过,它比我想要的慢


我有两张单子。 第一个列表
filecolumnnames
是通过拆分带分隔符的字符串(行)创建的,并且会为文件中的每一行重新创建此列表。 第二个列表具有第一个列表需要重新排列和重新连接的顺序

这很有效

string fileName = "F:\\temp.csv";
//file data has first row col3,col2,col1,col0; 
//second row: 4,3,2,1
//so on

string fileName_recreated = "F:\\temp_1.csv";

int count = 0;
const Int32 BufferSize = 1028;
using (var fileStream = File.OpenRead(fileName))
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize))
{
    String line;
    List<int> list = new List<int>();
    string orderedcolumns = "\"\"";
    string tableheader = "col0,col1,col2,col3,col4,col5,col6,col7,col8,col9,col10";
    List<string> tablecolumnnames = new List<string>();
    List<string> filecolumnnames = new List<string>();
    while ((line = streamReader.ReadLine()) != null)
    {
        count = count + 1;
        StringBuilder sb = new StringBuilder("");
        tablecolumnnames = tableheader.Split(',').ToList();
        if (count == 1)
        {
            string fileheader = line;
            //fileheader=""col2,col1,col0"
            filecolumnnames = fileheader.Split(',').ToList();

            foreach (string col in tablecolumnnames)
            {
                int index = filecolumnnames.IndexOf(col);

                if (index == -1)
                {
                    sb.Append(",");
                   // orderedcolumns=orderedcolumns+"+\",\"";
                   list.Add(-1);
                }
                else
                {
                    sb.Append(filecolumnnames[index] + ",");
                    //orderedcolumns = orderedcolumns+ "+filecolumnnames["+index+"]" + "+\",\"";

                       list.Add(index);
                }

                // MessageBox.Show(orderedcolumns);
            }
        }
        else
        {

            filecolumnnames = line.Split(',').ToList();

            foreach (int items in list)
            {

                //MessageBox.Show(items.ToString());

                if (items == -1)
                {
                    sb.Append(",");
                }
                else
                {
                    sb.Append(filecolumnnames[items] + ",");

                }


            }
           //expected format sb.Append(filecolumnnames[3] + "," + filecolumnnames[2] + "," + filecolumnnames[2] + ",");
            //sb.Append(orderedcolumns);

            var result = String.Join (", ", list.Select(index => filecolumnnames[index]));
        }



            using (FileStream fs = new FileStream(fileName_recreated, FileMode.Append, FileAccess.Write))
            using (StreamWriter sw = new StreamWriter(fs))
            {
                sw.WriteLine(sb.ToString());
            }


    }
string fileName=“F:\\temp.csv”;
//文件数据有第一行col3、col2、col1、col0;
//第二排:4,3,2,1
//诸如此类
字符串文件名\u重新创建=“F:\\temp\u 1.csv”;
整数计数=0;
const Int32 BufferSize=1028;
使用(var fileStream=File.OpenRead(文件名))
使用(var streamReader=newstreamreader(fileStream,Encoding.UTF8,true,BufferSize))
{
弦线;
列表=新列表();
字符串orderedcolumns=“\”\”;
string tableheader=“col0、col1、col2、col3、col4、col5、col6、col7、col8、col9、col10”;
List tablecolumnnames=新列表();
List filecolumnnames=新列表();
而((line=streamReader.ReadLine())!=null)
{
计数=计数+1;
StringBuilder sb=新的StringBuilder(“”);
tablecolumnnames=tableheader.Split(',').ToList();
如果(计数=1)
{
字符串fileheader=行;
//fileheader=”“col2、col1、col0”
filecolumnnames=fileheader.Split(',').ToList();
foreach(tablecolumnnames中的字符串列)
{
int index=filecolumnnames.IndexOf(col);
如果(索引==-1)
{
某人加上(“,”);
//orderedcolumns=orderedcolumns+“+\”,\”;
增加(-1);
}
其他的
{
sb.追加(文件列名[索引]+“,”);
//orderedcolumns=orderedcolumns+“+文件列名[“+索引+”]“+”+“\”,\”;
列表。添加(索引);
}
//显示(orderedcolumns);
}
}
其他的
{
filecolumnnames=line.Split(',').ToList();
foreach(列表中的int项)
{
//Show(items.ToString());
如果(项目==-1)
{
某人加上(“,”);
}
其他的
{
sb.追加(文件列名[项目]+“,”);
}
}
//预期格式sb.Append(filecolumnnames[3]+“,“+filecolumnnames[2]+”,“+filecolumnnames[2]+”,”);
//sb.追加(有序列);
var result=String.Join(“,”,list.Select(index=>filecolumnnames[index]);
}
使用(FileStream fs=newfilestream(fileName\u重新创建,FileMode.Append,FileAccess.Write))
使用(StreamWriter sw=新StreamWriter(fs))
{
sw.WriteLine(sb.ToString());
}
}
我试图通过构造一个字符串
orderedcolumns
,并删除每行发生的第二个for-each循环,并用构造的字符串替换它,从而使其更快。 因此,如果您取消对orderedcolumns字符串构造的注释
orderedcolumns=orderedcolumns+“+filecolumnnames[“+index+”]“+”+\”,\”;
并取消对append
sb.append(orderedcolumns);
的注释,我希望在构造的字符串中包含该值,但当我追加orderedcolumns时,它会追加文本,即。
“+”、“+filecolumnnames[3]+”、“+filecolumnnames[2]+”、“+filecolumnnames[1]+”、“+filecolumnnames[0]+”、“+”、“+”、“+”、“+”、“+”、“

i、 e.我希望它接受filecolumnnames[3]列表中的值,而不是filecolumnnames[3]名称本身

期望值:如果该行有1,2,3,4
我希望输出为4,3,2,1,因为filecolumnnames[3]将有4个,filecolumnnames[2]将有3个..

字符串。Join
是从序列构造逗号/空格分隔字符串的方法

var result = String.Join (", ", list.Select(index => filecolumnnames[index]);
由于您只读取输入和输出中的列子集和顺序不匹配,所以我将使用字典保存每一行输入

 var row = tablecolumnnames
       .Zip(line.Split(','), (Name,Value)=> new {Name,Value})
       .ToDictionary(x => x.Name, x.Value);
对于输出,我将从默认值或输入行填充序列:

 var outputLine = String.Join(",",
      filecolumnnames
         .Select(name => row.ContainsKey(name) ? row[name] : ""));
注意:代码是键入的,而不是编译的

orderedcolumns = orderedcolumns+ "+filecolumnnames["+index+"]" + "+\",\""; "   
应该是

orderedcolumns = orderedcolumns+ filecolumnnames[index] + ",";   
但是,您应该像其他人指出的那样使用join。或者

orderedcolumns.AppendFormat("{0},", filecolumnnames[index]); 

请不要加上“谢谢”和其他与问题不直接相关的文本(如“新到C,站点,……”)。如果你觉得感谢对你的问题很重要-请先讨论(但先考虑阅读)。。更正后,您的代码不会更快。更正后的代码最终看起来像您的原始代码。如果orderedcolumns是一个字符串,则修改我的代码以使其与我尝试实现的完全一致会慢得多Sam,请检查我的编辑是否反映了您的要求。是的alex,这是我尝试实现的总体目标。我尝试过这样做,但我的列表中可能有比实际列更多的列名,在这种情况下,我必须将其设置为“,",修改了我的question@sam您真的应该在文章中提供完整的示例,所有涉及的内容都内联有常量值comment@sam那么实际的预期输出呢?我怀疑简单地添加一组空y列就足够了(比如
list.Select(index=>filecolumn
orderedcolumns.AppendFormat("{0},", filecolumnnames[index]);