文本文件读写排序列表中的另一个文件 [英] text file reading and writing into another file in sorted list

查看:85
本文介绍了文本文件读写排序列表中的另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其数据具有以下格式

I have one text file having the data in the below format

id coun	desc
AA| 12|Test
BB | 15|Test1
CC| 10|Test2
AA|20 |Test3


...
...
...
...



以这种格式,第一个值(AA)(列名ID)是一些名称,第二个值(12)(列名计数)是计数,第三个值是一些描述(列名desc).所以在我的要求下,我想阅读该文本文件,并希望将第一行的第一列与第一列的所有其他行进行检查,如果找到匹配项,则应将第一列的计数添加到match列的count.ie
第一行的AA值与第四行的第一列匹配,然后第一列的计数将为32(12 +20),就像该过程将重复到文件末尾一样,一旦完成此过程,则需要将该数据写入一些文本文件,该文件将基于count列进行排序.

请帮帮我.



in this format first value(AA)(column name id)is some name and second value (12) (column name count)is count and the third value is some description (column name desc).so in my requirement i want to read that text file and want to check with the first column in the first line with all other lines in the first column ,if any match is found then the count of the first column should be added to the match column''s count.i.e.
AA in first line value is matched with fourth line first column then count of the first column will be 32 (12 +20) like that procee will repeat till the end of the file , once this process is done need to write this data into some text file that would be in the sorted order based on the count column.

please help me out.

推荐答案

这是蛮力方法-但它简单,灵活且易于理解:

This is the brute force approach - but it is simple, flexible, and easy to understand:

            string[] lines = File.ReadAllLines(filePath);
            string header = lines[0];
            lines[0] = null;      //Remove the header line
            SortedDictionary<string, Row> rows = new SortedDictionary<string, Row>();
            foreach (string s in lines)
                {
                string line = s.Trim();
                if (!string.IsNullOrEmpty(line))
                    {
                    Row r = new Row(line, '|');
                    if (!rows.ContainsKey(r.Id))
                        {
                        rows.Add(r.Id, r);
                        }
                    else
                        {
                        rows[r.Id].Count += r.Count;
                        }
                    }
                }

... Then write your file!

    public class Row
        {
        private int count;      // Property base for Count: cannot use a property as an out parameter
        public string Id { get; set; }
        public int Count { get { return count; } set { count = value; } }
        public string Description { get; set; }
        public Row(string line, char sep)
            {
            string[] parts = line.Split(sep);
            if (parts.Length == 3)
                {
                Id = parts[0].Trim();
                Description = parts[2].Trim();
                if (int.TryParse(parts[1].Trim(), out count))
                    {
                    return;
                    }
                }
            throw new ApplicationException("Unable to parse line: " + line);
            }
        }


这篇关于文本文件读写排序列表中的另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆