C# - 如何解析文本文件(空格分隔的数字)?

发布于 2024-11-08 06:42:08 字数 122 浏览 0 评论 0原文

给定一个由空格分隔的数据文件,

10 10 10 10 222 331 
2 3 3 4 45
4 2 2 4

如何读取该文件并加载到数组中

谢谢

Given a data file delimited by space,

10 10 10 10 222 331 
2 3 3 4 45
4 2 2 4

How to read this file and load into an Array

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

鯉魚旗 2024-11-15 06:42:08
var fileContent = File.ReadAllText(fileName);
var array = fileContent.Split((string[])null, StringSplitOptions.RemoveEmptyEntries);

如果您只有数字并且需要一个 int 列表作为结果,您可以这样做:

var numbers = array.Select(arg => int.Parse(arg)).ToList();
var fileContent = File.ReadAllText(fileName);
var array = fileContent.Split((string[])null, StringSplitOptions.RemoveEmptyEntries);

if you have numbers only and need a list of int as a result, you can do this:

var numbers = array.Select(arg => int.Parse(arg)).ToList();
迎风吟唱 2024-11-15 06:42:08

这取决于您想要的数组类型。如果您想将所有内容展平为一维数组,请使用 Alex Aza 的答案,否则,如果您想要一个映射到文本文件中的行和元素的二维数组:

var array = File.ReadAllLines(filename)
                .Select(line => line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                .Where(line => !string.IsNullOrWhiteSpace(line)) // Use this to filter blank lines.
                .Select(int.Parse) // Assuming you want an int array.
                .ToArray();

请注意,没有错误处理,所以如果解析失败,上面的代码将会抛出异常。

It depends on the kind of array you want. If you want to flatten everything into a single-dimensional array, go with Alex Aza's answer, otherwise, if you want a 2-dimensional array that maps to the lines and elements within the text file:

var array = File.ReadAllLines(filename)
                .Select(line => line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                .Where(line => !string.IsNullOrWhiteSpace(line)) // Use this to filter blank lines.
                .Select(int.Parse) // Assuming you want an int array.
                .ToArray();

Be aware that there is no error handling, so if parsing fails, the above code will throw an exception.

吃兔兔 2024-11-15 06:42:08

我无法得到快速乔·史密斯的工作答案,所以我修改了它。我将修改后的代码放入“FileReader”类中的静态方法中:

public static double[][] readWhitespaceDelimitedDoubles(string[] input)
{
    double[][] array = input.Where(line => !String.IsNullOrWhiteSpace(line)) // Use this to filter blank lines.
        .Select(line => line.Split((string[])null, StringSplitOptions.RemoveEmptyEntries))
        .Select(line => line.Select(element => double.Parse(element)))
        .Select(line => line.ToArray())
        .ToArray();

        return array;
}

对于我的应用程序,我解析的是 double 而不是 int。要调用代码,请尝试使用如下内容:

string[] fileContents = System.IO.File.ReadAllLines(openFileDialog1.FileName);
double[][] fileContentsArray = FileReader.readWhitespaceDelimitedDoubles(fileContents);

Console.WriteLine("Number of Rows:      {0,3}", fileContentsArray.Length);
Console.WriteLine("Number of Cols:      {0,3}", fileContentsArray[0].Length);

I couldn't get Quick Joe Smith's answer to work, so I modified it. I put the modified code into a static method within a "FileReader" class:

public static double[][] readWhitespaceDelimitedDoubles(string[] input)
{
    double[][] array = input.Where(line => !String.IsNullOrWhiteSpace(line)) // Use this to filter blank lines.
        .Select(line => line.Split((string[])null, StringSplitOptions.RemoveEmptyEntries))
        .Select(line => line.Select(element => double.Parse(element)))
        .Select(line => line.ToArray())
        .ToArray();

        return array;
}

For my application, I was parsing for double as opposed to int. To call the code, try using something like this:

string[] fileContents = System.IO.File.ReadAllLines(openFileDialog1.FileName);
double[][] fileContentsArray = FileReader.readWhitespaceDelimitedDoubles(fileContents);

Console.WriteLine("Number of Rows:      {0,3}", fileContentsArray.Length);
Console.WriteLine("Number of Cols:      {0,3}", fileContentsArray[0].Length);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文