csv 当值中包含少量字符串时访问列表值

px9o7tmv  于 2022-12-15  发布在  其他
关注(0)|答案(2)|浏览(133)

下面是代码:

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);         
                }
            }

它是从我的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

首先,为什么在行尾有很多逗号?其次,如果我想访问值“10”,该怎么办(这是第5个值),这可能吗?或者更进一步,我的任务是检查第5个值,如果是5,例如,我想取第5个值=5的每一行,并为它们创建一个csv,如果5thvalue=10,我想为这些记录创建一个csv,等等。但是一次只能执行一个任务,我如何访问这些值?

qq24tv8q

qq24tv8q1#

1:行尾逗号表示第一项为空""
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:然后可以获得值为fiveIndex的行列表

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

在您的示例中,第3行和第5行的值为10(index=2,index=4)。然后,您可以将这些行保存在csv file中。

7qhs6swi

7qhs6swi2#

最后做了:

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();
            }

使命就完成了。

相关问题