如何使用C#读取此文本文件并存储在列表中[英] How to Read This Text File and store in a list using C#

本文是小编为大家收集整理的关于如何使用C#读取此文本文件并存储在列表中的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

文本文件数据如下:

S.No    Name        Description Quantity    Rate    Discount    Amount
1       Apple       Friut is    12      24.02       0           242
                    Good for
                    health
2       Orange      Friut       5       12.22       3           128
3       Banana      Friut       5       12.22       3           128
4       Grapes      Friut       5       12.22       3           128

我想在列表中添加所有的行和列,但是说明列中有多个行.我该如何解决这个问题.我在这里添加我现有的代码:

我现有的代码如下:

class Program
{
    static void Main(string[] args)
    {
        var dd = File.ReadAllLines(                    
                "C:\\Users\\Trainee\\Desktop\\Saravanan_Test\\27.8.2018\\Inputfile.txt")
                     .Skip(1)
                     .Where(s => s.Length > 1)
                     .Select(x => splits(x)).ToList();

        foreach (var item in dd)
        {
            Console.WriteLine(item.id+"\t" 
                              + item.Name+"\t"
                              + item.Description+"\t"
                              + item.Quantity+"\t"
                              + item.Rate+"\t"
                              + item.Discount+"\t"
                              + item.Amount);
        }

        Console.ReadKey();

    }

    private static Class1 splits(string x)
    {
        var columns = x.Split('\t').Where(c => c != "").ToList();
        return new Class1
        {
            id = Convert.ToInt32(columns[0]),
            Name = columns[1],
            Description = columns[2],
            Quantity = Convert.ToInt32(columns[3]),
            Rate = Convert.ToDouble(columns[4]),
            Discount = Convert.ToInt32(columns[5]),
            Amount = int.Parse(columns[6])
        };
    }
}    

class Class1
{
    public int id { get; set; }
    public string Name { get; set; }
    public String Description { get; set; }
    public int Quantity { get; set; }
    public double Rate { get; set; }
    public int Discount { get; set; }
    public int Amount { get; set; }
}

我想将数据存储到列表中,例如:

list.Add(new{ sno=1, Name="Apple", 
              Description="Friut is good for Health", 
              Quantity=12, Rate=24.02, Discount=0,
              Amount=242 });

预先感谢.

推荐答案

注意:此解决方案基于所讨论的文件.数据被空间分开,不建议使用格式.回答以帮助他具有内容格式的人.测试和工作.

static void Main(string[] args)
{
    List<Data> list = new List<Data>();

    var dd = File.ReadAllLines(@"C:\Users\XXXX\Desktop\test.txt")
     .Skip(1)
     .Where(s => s.Length > 1).ToList();

    foreach (var item in dd)
    {
        var columns = item.Split('\t').Where(c => c.Trim() != string.Empty).ToList();

        if (columns != null && columns.Count > 0)
        {
            int id;

            if (int.TryParse(columns[0], out id))
            {
                list.Add(new Data()
                {
                    id = Convert.ToInt32(columns[0]),
                    Name = columns[1],
                    Description = columns[2],
                    Quantity = Convert.ToInt32(columns[3]),
                    Rate = Convert.ToDouble(columns[4]),
                    Discount = Convert.ToInt32(columns[5]),
                    Amount = int.Parse(columns[6])
                });
            }
            else
            {
                list.Last().Description += columns[0];
            }
        }
    }

    Console.ReadLine();
}

本文地址:https://www.itbaoku.cn/post/1556816.html

问题描述

The Text File Data is Like Below:

S.No    Name        Description Quantity    Rate    Discount    Amount
1       Apple       Friut is    12      24.02       0           242
                    Good for
                    health
2       Orange      Friut       5       12.22       3           128
3       Banana      Friut       5       12.22       3           128
4       Grapes      Friut       5       12.22       3           128

I want to add all the Rows& Columns in list but Description column have multiple Rows in single item. How can I Solve this. I add My Existing Code Here:

My Existing Code is as follows:

class Program
{
    static void Main(string[] args)
    {
        var dd = File.ReadAllLines(                    
                "C:\\Users\\Trainee\\Desktop\\Saravanan_Test\\27.8.2018\\Inputfile.txt")
                     .Skip(1)
                     .Where(s => s.Length > 1)
                     .Select(x => splits(x)).ToList();

        foreach (var item in dd)
        {
            Console.WriteLine(item.id+"\t" 
                              + item.Name+"\t"
                              + item.Description+"\t"
                              + item.Quantity+"\t"
                              + item.Rate+"\t"
                              + item.Discount+"\t"
                              + item.Amount);
        }

        Console.ReadKey();

    }

    private static Class1 splits(string x)
    {
        var columns = x.Split('\t').Where(c => c != "").ToList();
        return new Class1
        {
            id = Convert.ToInt32(columns[0]),
            Name = columns[1],
            Description = columns[2],
            Quantity = Convert.ToInt32(columns[3]),
            Rate = Convert.ToDouble(columns[4]),
            Discount = Convert.ToInt32(columns[5]),
            Amount = int.Parse(columns[6])
        };
    }
}    

class Class1
{
    public int id { get; set; }
    public string Name { get; set; }
    public String Description { get; set; }
    public int Quantity { get; set; }
    public double Rate { get; set; }
    public int Discount { get; set; }
    public int Amount { get; set; }
}

I want to store data into list like:

list.Add(new{ sno=1, Name="Apple", 
              Description="Friut is good for Health", 
              Quantity=12, Rate=24.02, Discount=0,
              Amount=242 });

Thanks in Advance.

推荐答案

NOTE: This solution is based on the file shared in question. Data is separated by spaces and format is not advisable to use. Answering to help person with content format he has. Tested and working.

static void Main(string[] args)
{
    List<Data> list = new List<Data>();

    var dd = File.ReadAllLines(@"C:\Users\XXXX\Desktop\test.txt")
     .Skip(1)
     .Where(s => s.Length > 1).ToList();

    foreach (var item in dd)
    {
        var columns = item.Split('\t').Where(c => c.Trim() != string.Empty).ToList();

        if (columns != null && columns.Count > 0)
        {
            int id;

            if (int.TryParse(columns[0], out id))
            {
                list.Add(new Data()
                {
                    id = Convert.ToInt32(columns[0]),
                    Name = columns[1],
                    Description = columns[2],
                    Quantity = Convert.ToInt32(columns[3]),
                    Rate = Convert.ToDouble(columns[4]),
                    Discount = Convert.ToInt32(columns[5]),
                    Amount = int.Parse(columns[6])
                });
            }
            else
            {
                list.Last().Description += columns[0];
            }
        }
    }

    Console.ReadLine();
}