C#:从txt文件中读取数据

发布于 2024-10-02 17:22:19 字数 304 浏览 5 评论 0原文

我有一个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假面具 2024-10-09 17:22:19

好吧,您可以从 File.ReadAllLines 开始() 方法。然后,迭代该文件中的行,检查它们是否与模式匹配。如果他们这样做,提取必要的文本并用它做任何你想做的事。

下面是一个示例,假设您想要格式为 [(field 1)][(field 2)] 的行:

// Or wherever your file is located
string path = @"C:\MyFile.edf";

// Pattern to check each line
Regex pattern = new Regex(@"\[([^\]]*?)\]");

// Read in lines
string[] lines = File.ReadAllLines(path);

// Iterate through lines
foreach (string line in lines)
{
   // Check if line matches your format here
   var matches = pattern.Matches(line);

   if (matches.Count == 2)
   {
      string value1 = matches[0].Groups[1].Value;
      string value2 = matches[1].Groups[1].Value;

      Console.WriteLine(string.Format("{0} - {1}", value1, value2));
   }
}

这会将它们输出到控制台窗口,但您显然可以使用 value1value2 (将它们写入另一个文件,将它们存储在数据结构中,等等)。

另外,请注意正则表达式不是我的强项——可能有一种更优雅的方法来检查一行是否与您的模式匹配:)

如果您想了解更多信息,请查看 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)]:

// Or wherever your file is located
string path = @"C:\MyFile.edf";

// Pattern to check each line
Regex pattern = new Regex(@"\[([^\]]*?)\]");

// Read in lines
string[] lines = File.ReadAllLines(path);

// Iterate through lines
foreach (string line in lines)
{
   // Check if line matches your format here
   var matches = pattern.Matches(line);

   if (matches.Count == 2)
   {
      string value1 = matches[0].Groups[1].Value;
      string value2 = matches[1].Groups[1].Value;

      Console.WriteLine(string.Format("{0} - {1}", value1, value2));
   }
}

This outputs them to the console window, but you could obviously do whatever you want with value1 and value2 (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.

森林迷了鹿 2024-10-09 17:22:19

让我们假设您的文件确实像您描述的那样简单。然后你可以删除第一行并解析数据行,如下所示:

foreach (string line in File.ReadAllLines(@"C:\MyFile.edf").Skip(1))
{
    var parts = line.Split("][");
    var value1 = parts[0].Replace("[", "");
    var value2 = parts[1].Replace("]", "");

    Console.WriteLine(string.Format("{0} - {1}", value1, value2));
}

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:

foreach (string line in File.ReadAllLines(@"C:\MyFile.edf").Skip(1))
{
    var parts = line.Split("][");
    var value1 = parts[0].Replace("[", "");
    var value2 = parts[1].Replace("]", "");

    Console.WriteLine(string.Format("{0} - {1}", value1, value2));
}
寂寞笑我太脆弱 2024-10-09 17:22:19

另一种变化。

var lines = File.ReadAllLines(file)
    .Skip(1)
    .Select(x => x.Split(new[] { '[', ']' }, 
        StringSplitOptions.RemoveEmptyEntries));
foreach(var pair in lines)
{
    Console.WriteLine(pair.First()+" - "+pair.Last());
}

Another variation.

var lines = File.ReadAllLines(file)
    .Skip(1)
    .Select(x => x.Split(new[] { '[', ']' }, 
        StringSplitOptions.RemoveEmptyEntries));
foreach(var pair in lines)
{
    Console.WriteLine(pair.First()+" - "+pair.Last());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文