如何解析文本文件?

发布于 2024-08-18 02:47:46 字数 422 浏览 2 评论 0原文

基本上,我需要有人帮助我或向我展示代码,使我能够从我称为 c1.txt 的文件中读取名称和价格。

这是我已经拥有的。

    TextReader c1 = new StreamReader("c1.txt");
        if (cse == "c1")
        {
            string compc1;
            compc1 = c1.ReadLine();
            Console.WriteLine(compc1);
            Console.WriteLine();
            compcase = compc1;
            compcasecost = 89.99;
        }

另外,如何选择从文本文档中读取的行也很棒。

Basically I need someone to help me or show me the code that will allow me to read a name and a price from a file i have called c1.txt.

This is what i already have.

    TextReader c1 = new StreamReader("c1.txt");
        if (cse == "c1")
        {
            string compc1;
            compc1 = c1.ReadLine();
            Console.WriteLine(compc1);
            Console.WriteLine();
            compcase = compc1;
            compcasecost = 89.99;
        }

also how to select a line to read from a text document would be great.

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

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

发布评论

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

评论(3

感性不性感 2024-08-25 02:47:46

您还没有告诉我们文本文件的格式。我将假设以下内容:

Milk|2.69
Eggs|1.79
Yogurt|2.99
Soy milk|3.79

您也没有指定输出。我将假设以下内容:

Name = Milk, Price = 2.69
Name = Eggs, Price = 1.79
Name = Yogurt, Price = 2.99
Name = Soy milk, Price = 3.79

然后以下内容将读取这样的文件并产生所需的输出。

using(TextReader tr = new StreamReader("c1.txt")) {
    string line;
    while((line = tr.ReadLine()) != null) {
        string[] fields = line.Split('|');
        string name = fields[0];
        decimal price = Decimal.Parse(fields[1]);
        Console.WriteLine(
            String.Format("Name = {0}, Price = {1}", name, price)
        );
    }
}

如果您的分隔符不同,那么您需要将参数 '|' 更改为方法 String.Split (在名为 String 的实例上调用) lineline.Split('|'))。

如果您的格式需要不同,那么您需要使用“

String.Format("Name = {0}, Price = {1}", name, price)

如果您有任何问题请告诉我”这一行。

You haven't told us the format of the text file. I am going to assume the following:

Milk|2.69
Eggs|1.79
Yogurt|2.99
Soy milk|3.79

You also didn't specify the output. I am going to assume the following:

Name = Milk, Price = 2.69
Name = Eggs, Price = 1.79
Name = Yogurt, Price = 2.99
Name = Soy milk, Price = 3.79

Then the following will read such a file and produce the desired output.

using(TextReader tr = new StreamReader("c1.txt")) {
    string line;
    while((line = tr.ReadLine()) != null) {
        string[] fields = line.Split('|');
        string name = fields[0];
        decimal price = Decimal.Parse(fields[1]);
        Console.WriteLine(
            String.Format("Name = {0}, Price = {1}", name, price)
        );
    }
}

If your separator is different then you need to change the parameter '|' to the method String.Split (invoked on the instance of String named line as line.Split('|')).

If your format needs to be different then you need to play with the line

String.Format("Name = {0}, Price = {1}", name, price)

Let me know if you have any questions.

请你别敷衍 2024-08-25 02:47:46

您还可以尝试使用解析帮助器类作为起点,例如 http://www.blackbeltcoder.com/Articles/strings/a-text-parsing-helper-class

You can also try using a parsing helper class as a starting point, such as the one described at http://www.blackbeltcoder.com/Articles/strings/a-text-parsing-helper-class.

如何视而不见 2024-08-25 02:47:46
    static void ReadText()
    {
        //open the file, read it, put each line into an array of strings
        //and then close the file
        string[] text = File.ReadAllLines("c1.txt");

        //use StringBuilder instead of string to optimize performance
        StringBuilder name = null;
        StringBuilder price = null;
        foreach (string line in text)
        {
            //get the name of the product (the string before the separator "," )
            name = new StringBuilder((line.Split(','))[0]);
            //get the Price (the string after the separator "," )
            price = new StringBuilder((line.Split(','))[1]);

            //finally format and display the result in the Console
            Console.WriteLine("Name = {0}, Price = {1}", name, price);
        }

它给出的结果与@Jason 的方法相同,但我认为这是一个优化版本。

    static void ReadText()
    {
        //open the file, read it, put each line into an array of strings
        //and then close the file
        string[] text = File.ReadAllLines("c1.txt");

        //use StringBuilder instead of string to optimize performance
        StringBuilder name = null;
        StringBuilder price = null;
        foreach (string line in text)
        {
            //get the name of the product (the string before the separator "," )
            name = new StringBuilder((line.Split(','))[0]);
            //get the Price (the string after the separator "," )
            price = new StringBuilder((line.Split(','))[1]);

            //finally format and display the result in the Console
            Console.WriteLine("Name = {0}, Price = {1}", name, price);
        }

It gives the same results as @Jason's method, but I think this is an optimized version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文