简体   繁体   English

从文件获取数据,然后使用c#将其统一转换为字典

[英]Get data from a file and convert it to dictionary in unity using c#

i am trying to read from a file and convert it into dictionary in unity using c#.The file contains data like 我正在尝试从文件中读取文件,然后使用c#将其统一转换为字典。文件中包含如下数据

1 1 1 acsbd 
1 2 1 123ws 

here i want to make the first 6 characters to the key and rest characters as values. 在这里我想使键的前6个字符和其余字符作为值。

here is the code i tried(it is mostly from stackoverflow) 这是我尝试过的代码(主要来自stackoverflow)

System.IO.StreamReader file = new System.IO.StreamReader (
  @"D:\Programming\Projects\Launch pad\itnol\KeySound");


     while ((line = file.ReadLine()) != null)
     {
         char[] line1 = line.ToCharArray();
         if (line1.Length >= 11)
         {
             line1[5] = ':';
             line = line1.ToString();
             //Console.WriteLine(line);
         }
         var items = line.Split(new[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries)
             .Select(s => s.Split(new[] { ':' }));

         Dictionary<string, string> dict = new Dictionary<string, string>();
         foreach (var item in items)
         {
             Debug.Log(item[0]);
             dict.Add(item[0], item[1]);
         }

It complies but gives IndexOutOfRangeException exception thrown on run time 它符合但给了运行时抛出的IndexOutOfRangeException 异常

Thank you. 谢谢。

Try using Linq : 尝试使用Linq

using System.IO;
using System.Linq;

...

string fileName = @"D:\Programming\Projects\Launch pad\itnol\KeySound";

...

Dictionary<string, string> dict = File 
  .ReadLines(fileName)    
  .Where(line => line.Length >= 11)           // If you want to filter out lines 
  .ToDictionary(line => line.Substring(0, 6), // Key:   first 6 characters
                line => line.Substring(6));   // Value: rest characters

Edit : No Linq , no File version: 编辑 :没有Linq ,没有File版本:

string fileName = @"D:\Programming\Projects\Launch pad\itnol\KeySound";

...

Dictionary<string, string> dict = new Dictionary<string, string>();

// Do not forget to wrap IDisposable into using
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileName)) {
  while (true) {
    string line = reader.ReadLine();

    if (null == line)
      break;
    else if (line.Length >= 11) 
      dict.Add(line.Substring(0, 6), line.Substring(6));
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Linq 将 JSON 文件中两个不同位置的数据转换为 C# 字典中的键(字符串)和值(双精度)? - How to convert data from two different locations in JSON file to a key(string) and value(double) within a C# dictionary, using Linq? LINQ:使用C#从Dictionary中获取数据 - LINQ: Get the data from the Dictionary in c# 从CSV获取数据到字典C# - Get data from csv to dictionary c# 无法使用 .Split (C# Unity3D) 将数据从文本文件转换为数组 - Cannot convert data from textfile to array using .Split (C# Unity3D) 如何使用C#在Unity中使用JsonUtility从给定的JSON格式获取数据? - How to get data from the given JSON format using JsonUtility in Unity using C#? Unity 独立播放器 Windows 桌面平台 - 如何使用 Unity C# 从双击的文件中获取路径? - Unity Standalone Player Windows Desktop Platform- How to get path from file that is double click using Unity C#? 如何在Unity / C#中从Firebase实时数据库获取数据 - How to get data from Firebase Realtime Database in Unity / C# 从其他脚本文件变量Unity C#获取价值 - Get value from other script file variable Unity C# 如何从 Firebase unity c# 中的数据快照中获取密钥? - How to get the Key from data snapshot in Firebase unity c#? 从InputField获取数据并乘以Float? (Unity / C#) - Get data from InputField and Multiply by Float? (Unity/C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM