查找csv文件中每列的最大长度

时间:2017-10-30 09:12:18

标签: c# asp.net .net linq

所以,我试图在控制台应用程序中呈现一个csv文档。但是,由于文本大小不同,输出的格式不明显。

为了呈现它,我尝试计算每列的最大文本长度,然后在该列的剩余文本中附加空格,以便每列中的字符长度相等。

我试图获得字符数,但似乎无法弄清楚如何继续进行。

var file = File.ReadAllLines(@"E:\File.csv");
var lineList = file.Select(x => x.Split(',').ToList()).ToList();
int maxColumn = lineList.Select(x => x.Count).Max(x => x);
List<int> maxElementSize = new List<int>();
for (int i = 0; i < maxColumn; i++)
{
    //Some Logic    
}

任何帮助都将受到高度赞赏。

4 个答案:

答案 0 :(得分:2)

这是一个示例控制台应用程序,用于获取每列的最大字符长度:

driver.find_element_by_id("fileUploadField").send_keys(os.getcwd()+"/1.m4")

希望这会有所帮助。

答案 1 :(得分:1)

尝试使用嵌套for循环:

var inputLines = File.ReadAllLines(@"E:\File.csv");   
Dictionary<int,int> dictIndexLenght = new Dictionary<int,int>();
foreach(var line in inputLines)
{
    List<string> columList =  line.Split(',').ToList();
    for (int i = 0; i < columList.Count; i++)
    {
        int tempVal = 0;
        if(dictIndexLenght.TryGetValue(i,out tempVal))
        {
            if(tempVal<columList[i].Length)
            {
                dictIndexLenght[i]=columList[i].Length;
            }                  
        }
        else
            dictIndexLenght[i]=columList[i].Length;
    }

}

可以检查结果here或使用以下代码行:

for(int i=0;i<dictIndexLenght.Count;i++)
{
    Console.WriteLine("Column {0} : {1}", i, dictIndexLenght[i]);
}   

答案 2 :(得分:1)

在这里我是如何做到的,非常类似于非幸运的答案,只使用List<int>而不是Dictionary<int, int>。我添加了用于测试的虚拟数据,但是你可以看到读取文件的实际调用留在那里,所以你可以删除虚拟数据和读取它的行,它应该可以正常工作:

static void Main(string[] args)
{
    var fileLines = new List<string>
    {
        "Lorem, Ipsum, is, simply, dummy, text, of, the, printing, and, typesetting,",
        "industry., Lorem, Ipsum, has, been, the, industry's, standard, dummy, text,",
        "ever, since, the, 1500s, when, an, ",
        "unknown, printer, took, a, galley, of, type, and, scrambled, it, to, make,",
        "a, type, specimen, book.,",
        "It, has, survived, not, only, five, centuries, but, also, the, leap,",
        "into, electronic, typesetting, remaining, essentially, unchanged.,",
        "It, was, popularised, in, the, 1960s, with, the, release,",
        "of, Letraset, sheets, containing, Lorem, Ipsum, passages, and, more, ",
        "recently, with, desktop, publishing,",
        "software, like, Aldus, PageMaker, including, versions, of, Lorem, Ipsum."
    };

    var filePath = @"f:\public\temp\temp.csv";
    var fileLinesColumns = File.ReadAllLines(filePath).Select(line => line.Split(','));
    var colWidths = new List<int>();

    // Remove this line to use file data
    fileLinesColumns = fileLines.Select(line => line.Split(','));   

    // Get the max length of each column and add it to our list
    foreach (var fileLineColumns in fileLinesColumns)
    {
        for (int i = 0; i < fileLineColumns.Length; i++)
        {
            if (i > colWidths.Count - 1)
            {
                colWidths.Add(fileLineColumns[i].Length);
            }
            else if (fileLineColumns[i].Length > colWidths[i])
            {
                colWidths[i] = fileLineColumns[i].Length;
            }
        }
    }

    // Write out our columns, padding each one to match the longest line
    foreach (var fileLineColumns in fileLinesColumns)
    {
        for (int i = 0; i < fileLineColumns.Length; i++)
        {
            Console.Write(fileLineColumns[i].PadRight(colWidths[i]));
        }

        Console.WriteLine();
    }

    Console.Write("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

<强>输出

enter image description here

答案 3 :(得分:0)

初始化您的列表,然后遍历您的行,并在该行中循环遍历您的列:

for (i = 0; i < lineList.Count; i++)
{
    maxElementSize[i] = 0;
}
for (i = 0; i < lineList.Count; i++)
{
    for (j = 0; j < maxColumn; j++)
    {
        if(lineList[i][j].Length > maxElementSize[j])
           maxElementSize[j] = lineList[i][j].Length
    }
}