简体   繁体   English

如何在C#中解析文本文件?

[英]How to parse the text file in c#?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace readfrom_textfile {
    class Program {
        static void Main(string[] args) {
            var fileStream = new FileStream(@"D:\Practise\test.txt", FileMode.Open, FileAccess.Read);
            using(var streamReader = new StreamReader(fileStream, Encoding.UTF8)) {
                string line;
                while ((line = streamReader.ReadLine()) != null) {
                    string[] data = line.Split(new char[] {
                        '('
                    }, StringSplitOptions.RemoveEmptyEntries);
                    string bikeName = data[0].Trim().Split(' ').Last();
                    bikeName = bikeName.Remove(bikeName.Length - 1);
                    string[] usageData = data[1].Split(new char[] {
                        ';'
                    }, StringSplitOptions.RemoveEmptyEntries);
                    string issuedBike = usageData[0].Trim().Split(' ')[2];
                    string inUseBike = usageData[1].Trim().Split(' ')[2];
                    Console.WriteLine("Name:" + bikeName);
                    Console.WriteLine("Total:" + issuedBike);
                    Console.WriteLine("InUse:" + inUseBike);
                    Console.WriteLine();
                }

            }
            Console.ReadLine();
        }

In my text file the line is: 在我的文本文件中,该行是:

Users of Yamaha: (Total of 2 bike issued; Total of 2 bike in use).newline line1....................something[newline] line2..................something[newline] Bike details //emptyline 1125 Monica YamahaR15(v3.0)(petrol / 7788 4903), start Wed 1 / 18 8: 53 1128 Dhini Yamaha Fz(v2.0)(petrol / 7748 4903), start Wed 1 / 18 9: 53 Yamaha的用户:(已发布2辆自行车;正在使用2辆自行车).newline line1 ........... something [newline] line2 .... .............. something [newline]自行车详细信息// emptyline 1125 Monica YamahaR15(v3.0)(汽油/ 7788 4903),周三1/18 8:53 1128 Dhini Yamaha Fz (v2.0)(汽油/ 7748 4903),周三1/18 9:53开始

    This is the line.I want to get this line from my file and split into < pre >
        Name: Yamaha;
    Total: 1;
    Inuse: 1;
    members: Monica[Wed 1 / 18 8: 53], dhini[Wed 1 / 18 9: 53] < /pre>
    Like this format.

If you are storing each record in nextline in txt file (1 line per 1 record) like: 如果要将每个记录存储在txt文件的nextline中(每1条记录1行),例如:

Users of Yamaha: (Total of 1 bike issued; Total of 1 bike in use)
Users of Hero: (Total of 6 bike issued; Total of 2 bike in use)

then you can do using the following code which also stores the output in a file. 那么您可以使用以下代码来完成,该代码还将输出存储在文件中。

public void Main(string[] args)
{
   var fileStream = new FileStream(@"D:\Practise\test.txt", FileMode.Open, FileAccess.Read);
   using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
   {
      string line;
      string createTextFile = "";
      string outputPath = @"D:\Practise\Output.txt";
      while ((line = streamReader.ReadLine()) != null)
      {
         string[] data = line.Split(new char[] { '(' }, StringSplitOptions.RemoveEmptyEntries);
         string bikeName = data[0].Trim().Split(' ').Last();
         bikeName = bikeName.Remove(bikeName.Length - 1);
         string[] usageData = data[1].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
         string issuedBike = usageData[0].Trim().Split(' ')[2];
         string inUseBike = usageData[1].Trim().Split(' ')[2];

         Console.WriteLine("Name:" + bikeName);
         Console.WriteLine("Total:" + issuedBike);
         Console.WriteLine("InUse:" + inUseBike);
         Console.WriteLine();

         createTextFile += "Name:" + bikeName + Environment.NewLine;
         createTextFile += "Total:" + issuedBike + Environment.NewLine;
         createTextFile += "InUse:" + inUseBike + Environment.NewLine + Environment.NewLine;
      }
      File.WriteAllText(outputPath, createTextFile);
   }
   Console.ReadLine();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM