C# 从具有定义结构的文本文件创建C语言词典
我有一个包含以下信息的文本文件:C# 从具有定义结构的文本文件创建C语言词典,c#,C#,我有一个包含以下信息的文本文件: 01 Index home about 02 Home first second third 以数字开头的那一行表示一个键,紧跟其后直到一个空行,都是值 我想有一个字典对象,其中键作为第一行,后面的行作为字符串数组表示。 像这样: key = "01 Index" value = string[] 其中数组中的值为: string[0] = "home" string[1] = "about" 下一个键是02 Home,下面的几行是string[] 此方
01 Index
home
about
02 Home
first
second
third
以数字开头的那一行表示一个键,紧跟其后直到一个空行,都是值
我想有一个字典对象,其中键作为第一行,后面的行作为字符串数组表示。
像这样:
key = "01 Index"
value = string[]
其中数组中的值为:
string[0] = "home"
string[1] = "about"
下一个键是02 Home,下面的几行是string[]
此方法读取文本文件:
string[] ReadFileEntries(string path)
{
return File.ReadAllLines(path);
}
这给出了上面的示例中的所有行,在字符串[]中的8,其中第四行将是空白行。
如何从中创建所需的词典 提前谢谢注意。要将文件解析到词典中,您可以执行以下操作:
Dictionary<String, String[]> data = new Dictionary<String, String[]>();
String key = null;
List<String> values = new List<String>();
foreach (String line in File.ReadLines(path)) {
// Skip blank lines
if (String.IsNullOrEmpty(line))
continue;
// Check if it's a key (that should start from digit)
if ((line[0] >= '0' && line[0] <= '9')) { // <- Or use regular expression
if (!Object.ReferenceEquals(null, key))
data.Add(key, values.ToArray());
key = line;
values.Clear();
continue;
}
// it's a value (not a blank line and not a key)
values.Add(line);
}
if (!Object.ReferenceEquals(null, key))
data.Add(key, values.ToArray());
这应该做你想做的事。还有改进的余地,我建议重构一点
var result = new Dictionary<string, string[]>();
var input = File.ReadAllLines(@"c:\temp\test.txt");
var currentValue = new List<string>();
var currentKey = string.Empty;
foreach (var line in input)
{
if (currentKey == string.Empty)
{
currentKey = line;
}
else if (!string.IsNullOrEmpty(line))
{
currentValue.Add(line);
}
if (string.IsNullOrEmpty(line))
{
result.Add(currentKey, currentValue.ToArray());
currentKey = string.Empty;
currentValue = new List<string>();
}
}
if (currentKey != string.Empty)
{
result.Add(currentKey, currentValue.ToArray());
}
我们以这种方式声明该性质的词典:
Dictionary<string, string[]> myDict = new Dictionary<string, string[]>()
我认为像这样的事情可以做到:
string[] TheFileAsAnArray = ReadFileEntries(path);
Dictionary<string, string[]> myDict = new Dictionary<string, string[]>()
string key = "";
List<string> values = new List<string>();
for(int i = 0; i <= TheFileAsAnArray.Length; i++)
{
if(String.isNullOrEmpty(TheFileAsAnArray[i].Trim()))
{
myDict.Add(key, values.ToArray());
key = String.Empty;
values = new List<string>();
}
else
{
if(key == String.Empty)
key = TheFileAsAnArray[i];
else
values.Add(TheFileAsAnArray[i]);
}
}
你有什么错误?没有错误,我被ReadFileEntries方法卡住了。不明白为什么这会被标记为关闭???通常我们喜欢提问者在发帖前自己尝试。
string[] TheFileAsAnArray = ReadFileEntries(path);
Dictionary<string, string[]> myDict = new Dictionary<string, string[]>()
string key = "";
List<string> values = new List<string>();
for(int i = 0; i <= TheFileAsAnArray.Length; i++)
{
if(String.isNullOrEmpty(TheFileAsAnArray[i].Trim()))
{
myDict.Add(key, values.ToArray());
key = String.Empty;
values = new List<string>();
}
else
{
if(key == String.Empty)
key = TheFileAsAnArray[i];
else
values.Add(TheFileAsAnArray[i]);
}
}