简体   繁体   English

当值中的字符串很少时访问列表值

[英]Accessing List values when having few strings inside a value

So I have the following code:所以我有以下代码:

  void ReadFromCsv()
            {

                using (var reader = new StreamReader(@"d:\test.csv", Encoding.Default))
                {
                    List<string> listA = new List<string>();
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        var values = line.Split(';');                    
                        listA.Add(values[0]);                      
                    }
                    Console.WriteLine(listA);         
                }
            }

which is reading from my csv file and an example of a line I get is:这是从我的 csv 文件中读取的,我得到的一行示例是:

50,2,10,201,10,9090339,24-OCT-21 09.38.38.679000 AM,123456789,24/10/2021 09:39:23,22/10/2021 09:39:37,Sm123456789-SM-20211031-VSR-000123.pdf,,,,,26/01/2022 13:08:58,,2,,0

first of all, why are there many commas around the end of the line?首先,为什么行尾有很多逗号? second of all, what if I wanted to access the value "10" (which is the 5th value ) of that string line, is that possible?, or going further, my task is to check for that 5th value and if its 5 for example, I'd want to take every row with 5thvalue=5 and create a csv for them, if 5thvalue=10 I want to create a csv for those records, and so on.其次,如果我想访问该字符串行的值“10”(这是第 5 个值)怎么办?或者更进一步,我的任务是检查第 5 个值以及它的 5 是否为例如,我想获取 5thvalue=5 的每一行并为它们创建一个 csv,如果 5thvalue=10 我想为这些记录创建一个 csv,依此类推。 but one task at a time, how do I access that value?但是一次一个任务,我如何访问该值?

1: commas around the end of the line mean first item of lines is empty "" 1:行尾的逗号表示行的第一项为空""

2: you can get 5th value as below: 2:你可以得到如下第5个值:

 string _list = "50,2,10,201,10,9090339,24-OCT-21 09.38.38.679000 AM,123456789,24/10/2021 09:39:23,22/10/2021 09:39:37,Sm123456789-SM-20211031-VSR-000123.pdf,,,,,26/01/2022 13:08:58,,2,,0";

 var fiveIndex = _list.Split(',')[4];

3: then you can get list of lines that have a value of fiveIndex 3:然后您可以获得值为fiveIndex的行列表

var result =_list.Split(',').Select((v, i) => new { value = v, index = i }).Where(item => item.value == fiveIndex);

In your example, line 3 and line 5 have a value of 10(index=2, index=4).在您的示例中,第 3 行和第 5 行的值为 10(索引=2,索引=4)。 Then you can save these lines in csv file .然后您可以将这些行保存在csv file中。

在此处输入图像描述

ended up doing:最后做了:


            string chargeMonth = DateTime.Now.ToString("yyyyMM");
            var fileCreationDate = DateTime.Now.ToString("yyyyMMdd");
            string fileCreationTime = DateTime.Now.ToString("HHmmss");
            string constVal = "MLL";
            string fileType = "HIYUV-CHEVRA";
            string[] values;
            string header, sumRow;
            string line, compId;
            string inputFile = "records.CSV";
            Dictionary<string, System.IO.StreamWriter> outputFiles = new Dictionary<string, System.IO.StreamWriter>();
            using (System.IO.StreamReader file = new System.IO.StreamReader("D:\\" + inputFile, Encoding.Default))
            {
                header = file.ReadLine();
                while ((line = file.ReadLine()) != null)
                {
                    values = line.Split(",".ToCharArray());
                    compId = values[3];
                    if (!outputFiles.ContainsKey(compId))
                    {
                        string outputFileName = constVal + "-" + fileType + "-" + (String.Format("{0:00000}", Int32.Parse(compId))) + "-" + chargeMonth + "-" + fileCreationDate + "-" + fileCreationTime + ".CSV";
                        outputFiles.Add(compId, new System.IO.StreamWriter("D:\\" + outputFileName));
                        outputFiles[compId].WriteLine(header);
                    }
                    outputFiles[compId].WriteLine(line);
                }
            }
            foreach (System.IO.StreamWriter outputFile in outputFiles.Values)
            {
                outputFile.Close();
            }

and the mission is done.任务完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM