【问题标题】:Splitting an array into 2 parts将数组拆分为 2 部分
【发布时间】:2013-10-20 15:02:49
【问题描述】:

我正在尝试读取这种格式的日志文件:

date | cost
date | cost
..ect

使用以下代码将文件读入数组:

string[] lines = File.ReadAllLines("log.txt");

我的问题是如何将数组分成每行 2 个部分,以便我可以将它们添加到 2 列的列表视图中?我在想也许一本字典会是一个好的开始..

【问题讨论】:

  • 更多代码显示您所做的工作将是获得更好响应的好方法
  • 您的问题被标记为“c”,但在您的问题中,您使用了一行 C# 代码。那么,您使用哪种语言?
  • @ProgramFOX C?我确定我标记为 C# :/ 哎呀,如果可以的话,我想我会重新标记
  • @ryyker 谢谢你:)

标签: c# dictionary multidimensional-array


【解决方案1】:

假设这是 C# 而不是 C,以下可能会满足您的需求:

public class LogEntry{

    public string Date;
    public string Cost;


    public LogEntry(string date,string cost){
        Date=date;
        Cost=cost;
    }

}

...

// Grab the lines from the file:
string[] lines = File.ReadAllLines("log.txt");

// Create our output set:
LogEntry[] logEntries=new LogEntry[lines.Length];

// For each line in the file:
for(int i=0;i<lines.Length;i++){
    // Split the line:
    string[] linePieces=lines[i].Split('|');

    // Safety check - make sure this is a line we want:
    if(linePieces.Length!=2){
        // No thanks!
        continue;
    }

    // Create the entry:
    logEntries[i]=new LogEntry( linePieces[0] , linePieces[1] );
}

// Do something with logEntries.

请注意,这种处理只能使用相对较小的日志文件来完成。 File.ReadAllLines("log.txt") 对于大文件变得非常低效,此时使用原始 FileStream 更合适。

【讨论】:

  • 旁注:安全检查可能会在 logEntries 数组中生成空值。可以将其转换为 List,但可能效果不佳。
【解决方案2】:
var lines = File.ReadAllLines("log.txt").Select(l=> l.Split('|'));
var dictionary= lines.ToDictionary(x => x[0], y => y[1]);

【讨论】:

    【解决方案3】:

    使用二维数组和string.Split('-')

    string[] lines = File.ReadAllLines("log.txt");
    //Create an array with lines.Length rows and 2 columns
    string[,] table = new string[lines.Length,2];
    for (int i = 0; i < lines.Length; i++)
    {
         //Split the line in 2 with the | character
         string[] parts = lines[i].Split('|');
         //Store them in the array, trimming the spaces off
         table[i,0] = parts[0].Trim();
         table[i,1] = parts[1].Trim();
    }
    

    现在您将拥有一个如下所示的数组:

    table[date, cost]
    

    您可以使用字典,因此如果您想改进它,只需查找日期即可。 编辑:正如@Damith所做的那样

    此外,使用LINQ,您可以将其简化为:

    var table = File.ReadAllLines("log.txt").Select(s => s.Split('|')).ToDictionary(k => k[0].TrimEnd(' '), v => v[1].TrimStart(' '));
    

    您现在可以轻松地从 LINQ 表达式中获取结果:

    foreach (KeyValuePair<string, string> kv in table)
    {
          Console.WriteLine("Key: " + kv.Key + " Value: " + kv.Value);
    }
    

    另请注意,如果您不需要文件中的空格,您可以省略 Trim()s

    【讨论】:

      【解决方案4】:

      因为这篇文章是最初标记的C :)
      这是一个 C 示例:

      使用如下所示的数据文件(我称之为 temp.txt):

      3/13/56 | 13.34
      3/14/56 | 14.14
      3/15/56 | 15.00
      3/16/56 | 16.56
      3/17/56 | 17.87
      3/18/56 | 18.34
      3/19/56 | 19.31
      3/20/56 | 20.01
      3/21/56 | 21.00  
      

      这段代码会读取它,将它解析成一个单独的 2 暗字符串数组,char col[2][80][20];

      #include <ansi_c.h>
      
      int main()
      {
          int i;
          char *buf;
          char line[260];
          char col[2][80][20];
         FILE *fp;
         fp = fopen("c:\\dev\\play\\temp.txt", "r");
         i=-1;
         while(fgets(line, 260, fp))
         {
              i++;
              buf = strtok(line, "|");
              if(buf) strcpy(col[0][i], buf);
              buf = strtok(NULL, "|");
              if(buf) strcpy(col[1][i], buf);
         }
         fclose(fp);
      
         return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-04
        • 2014-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-29
        • 2021-08-14
        • 2014-10-23
        相关资源
        最近更新 更多