【问题标题】:How to read different types of data from text file?如何从文本文件中读取不同类型的数据?
【发布时间】:2012-02-04 13:50:01
【问题描述】:

我需要从文件中读取文本数据,其中每一行都有不同类型的数据。 所以,我创建了一个名为 subject 的大类。我的数据如下所示:

Subject name      M1   M2   M3   M4
Subject1           5    7    8    3
Old Subject        1    2    5    9

主要问题是,例如,是否可以读取第 1 行中的所有数据并将其分配给适当的字段,例如 SubjName = Subject1、M1 = 5、M2 = 7、M3 = 8 等等,没有使用子字符串? (在 C++ 中类似于流 >> Subject.SubjName; stream >> Subject.M1 = 5 等等)。

这是我的代码。

internal void Read()
{
        TextReader tr = new StreamReader("Data.txt");
        string line;
        while ((line = tr.ReadLine()) != null)    //read till end of line
        {
            tr.ReadLine();    //Skips the first line


        }

提前致谢

编辑:为了澄清,我希望字段是分隔的。

【问题讨论】:

  • 文件中的数据是如何存储的?任何字段分隔符?还是字段是固定长度的?
  • 至于您的编辑 - 既然您说您是写入文件的人,那么如何分隔字段完全取决于您。

标签: c# file text stream


【解决方案1】:

类似这个问题的解决方案可能会有所帮助,但显然使用制表符分隔符 (\t)

CSV to object model mapping

 from line in File.ReadAllLines(fileName).Skip(1)
    let columns = line.Split(',')
    select new
    {
      Plant = columns[0],
      Material = int.Parse(columns[1]),
      Density = float.Parse(columns[2]),
      StorageLocation = int.Parse(columns[3])
    }

【讨论】:

  • @Oded 我查看了 CSV 文件,因为它们大多是带有标题的文件,因此会有很多示例代码。
  • @SandeepBansal - 我的意思是关于 CSV 的问题没有任何内容。
【解决方案2】:

从您的问题中不清楚记录是如何存储在文件中的 - 字段是分隔的还是固定长度的。

无论如何 - 您可以使用 TextFieldParser 类,其中:

提供解析结构化文本文件的方法和属性。

它位于Microsoft.VisualBasic.dll 程序集中的Microsoft.VisualBasic.FileIO 命名空间中。

【讨论】:

    【解决方案3】:

    Split 和 Dictionary 以及您在此处选择的两种方法。您在您的行中阅读,将其用空格分隔,然后将其保存为字典中的名称/对象对。

    将以下代码放入 *.cs 文件中,然后构建并作为演示运行:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Collections;
    
    namespace stringsAsObjects
    {
    
        class stringObject
        {
            public static int Main(string[] args)
            {
                int counter = 0;
                string line;
    
                // Read the file and display it line by line.
                System.IO.StreamReader file =
                   new System.IO.StreamReader("Data.txt");
                string nameLine = file.ReadLine();
                string valueLine = file.ReadLine();
    
    
                file.Close();
    
                string[] varNames = nameLine.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                string[] varValues = valueLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                Dictionary<string, object> map = new Dictionary<string, object>();
    
                for(int i = 0; i<varNames.Length; i++)
                {
                    try
                    {
                        map[varNames[i]] = varValues[i];
                    }
                    catch (Exception ex)
                    {
                        map[varNames[i]] = null;
                    }
                }
    
                foreach (object de in map)
                {
                    System.Console.WriteLine(de);
                }
    
                Console.ReadKey();
                return 0;
    
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 2021-09-20
      相关资源
      最近更新 更多