C#:从txt文件中读取数据
我有一个 .EDF(文本)文件。该文件的内容如下:
ConfigFile.Sample, Software v0.32, CP Version 0.32
[123_Float][2]
[127_Number][0]
[039_Code][70]
我想要读取这些项目并按如下方式解析它们:
123_Float - 2
127_Number - 0
039_Code - 70
如何使用 C# 执行此操作?
I have an .EDF (text) file. The file's contents are as follows:
ConfigFile.Sample, Software v0.32, CP Version 0.32
[123_Float][2]
[127_Number][0]
[039_Code][70]
I wnat to read these items and parse them like this:
123_Float - 2
127_Number - 0
039_Code - 70
How can I do this using C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,您可以从
File.ReadAllLines 开始()
方法。然后,迭代该文件中的行,检查它们是否与模式匹配。如果他们这样做,提取必要的文本并用它做任何你想做的事。下面是一个示例,假设您想要格式为
[(field 1)][(field 2)]
的行:这会将它们输出到控制台窗口,但您显然可以使用
value1
和value2
(将它们写入另一个文件,将它们存储在数据结构中,等等)。另外,请注意正则表达式不是我的强项——可能有一种更优雅的方法来检查一行是否与您的模式匹配:)
如果您想了解更多信息,请查看 MSDN 上关于 从文本文件读取数据作为起点。
Well, you might start with the
File.ReadAllLines()
method. Then, iterate through the lines in that file, checking to see if they match a pattern. If they do, extract the necessary text and do whatever you want with it.Here's an example that assumes you want lines in the format
[(field 1)][(field 2)]
:This outputs them to the console window, but you could obviously do whatever you want with
value1
andvalue2
(write them to another file, store them in a data structure, etc).Also, please note that regular expressions are not my strong point -- there's probably a much more elegant way to check if a line matches your pattern :)
If you want more info, check out MSDN's article on reading data from a text file as a starting point.
让我们假设您的文件确实像您描述的那样简单。然后你可以删除第一行并解析数据行,如下所示:
Let us assume your file really is as simple as you describe it. Then you could drop the first line and parse the data lines like this:
另一种变化。
Another variation.