[英]C# - Getting multiple values with a single key, from a text file
I store multiple values that shares a single key on a text file. 我存储了多个在文本文件上共享单个键的值。 The text file looks like that:
文本文件看起来像这样:
Brightness 36 , Manual
BacklightCompensation 3 , Manual
ColorEnable 0 , None
Contrast 16 , Manual
Gain 5 , Manual
Gamma 122 , Manual
Hue 0 , Manual
Saturation 100 , Manual
Sharpness 2 , Manual
WhiteBalance 5450 , Auto
Now I want to store the int value & string value of each key (Brightness, for example). 现在我想存储每个键的int值和字符串值(例如,亮度)。
New to C# and could'nt find something that worked yet. C#的新手,无法找到有效的东西。
Thanks 谢谢
I'd recommend to use custom types to store these settings like these: 我建议使用自定义类型来存储这些设置,如下所示:
public enum DisplaySettingType
{
Manual, Auto, None
}
public class DisplaySetting
{
public string Name { get; set; }
public decimal Value { get; set; }
public DisplaySettingType Type { get; set; }
}
Then you could use following LINQ query using string.Split
to get all settings: 然后,您可以使用
string.Split
使用以下LINQ查询来获取所有设置:
decimal value = 0;
DisplaySettingType type = DisplaySettingType.None;
IEnumerable<DisplaySetting> settings = File.ReadLines(path)
.Select(l => l.Trim().Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries))
.Where(arr => arr.Length >= 3 && decimal.TryParse(arr[1], out value) && Enum.TryParse(arr[2], out type))
.Select(arr => new DisplaySetting { Name = arr[0], Value = value, Type = type });
With a regex and a little bit of linq you can do many things. 使用正则表达式和一点点linq,你可以做很多事情。
Here I assume you Know How to read a Text file . 在这里,我假设你知道如何阅读文本文件 。
Pros: If the file is not perfect, the reg exp will just ignore the misformatted line, and won't throw error. 优点:如果文件不完美,reg exp将忽略格式错误的行,并且不会抛出错误。
Here is a hardcode version of your file, note that a \\r
will appears because of it. 这是您文件的硬编码版本,请注意,因为它会出现
\\r
。 Depending on the way you read you file but it should not be the case with a File.ReadLines()
取决于您读取文件的方式,但不应该是
File.ReadLines()
string input =
@"Brightness 36 , Manual
BacklightCompensation 3 , Manual
ColorEnable 0 , None
Contrast 16 , Manual
Gain 5 , Manual
Gamma 122 , Manual
Hue 0 , Manual
Saturation 100 , Manual
Sharpness 2 , Manual
WhiteBalance 5450 , Auto";
string regEx = @"(.*) (\d+) , (.*)";
var RegexMatch = Regex.Matches(input, regEx).Cast<Match>();
var outputlist = RegexMatch.Select(x => new { setting = x.Groups[1].Value
, value = x.Groups[2].Value
, mode = x.Groups[3].Value });
/(.*) (\\d+) , (.*)/g
/(.*) (\\d+) , (.*)/g
1st Capturing Group (.*) 第一捕获组(。*)
.*
matches any character (except for line terminators) .*
匹配任何字符(行终止符除外)
*
Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy) *
量词 - 在零和无限次之间匹配,尽可能多次,根据需要回馈(贪婪) matches the character
匹配角色
literally (case sensitive)
字面意思(区分大小写)
2nd Capturing Group (\\d+) 第二捕获组(\\ d +)
\\d+
matches a digit (equal to [0-9]) \\d+
匹配一个数字(等于[0-9])
+
Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) +
量词 - 在一次和无限次之间匹配,尽可能多次,根据需要回馈(贪婪)
,
matches the characters ,
literally (case sensitive) ,
匹配字符,
字面意思(区分大小写)
3rd Capturing Group (.*) 第三捕获组(。*)
.*
matches any character (except for line terminators) .*
匹配任何字符(行终止符除外)
*
Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy) *
量词 - 在零和无限次之间匹配,尽可能多次,根据需要回馈(贪婪)
Never trust an input! 永远不要相信输入! Even if it's a file some other program did, or send by a customer.
即使它是某个其他程序所做的文件,也可能是客户发送的文件。
From my experience, you have then two ways of handeling bad format: 根据我的经验,你有两种处理不良格式的方法:
Read line by line, and register every bad line. 逐行读取,并记录每条坏线。
or Ignore them. 或者忽略它们。 You don't fit , you don't sit!
你不适合,你不坐!
And don't tell your self it won't happend, it will! 并且不要告诉你自己它不会发生,它会!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.