意外的“,”从 StreamReader 到 StreamWriter

Unexpected "," from StreamReader to StreamWriter

我已经尝试了很多东西以及研究和询问朋友,但似乎无法在没有“,”替换行的情况下写第二行。我所要做的就是在单独的文件中为每个项目读取一行。

每个读取的文件都有以下几项:

2/20/2014 7:33:10 AM
OPERATOR: jason
FILE: C:\ax14\Setups[=10=]0062363106RH.prt
UNITS: english
TEST RESULT: Pass

    CHANNEL 1
        TEST TYPE: VELOCITY
        RESULT:  Pass
        UPPER LIMIT: 0.2260
        LOWER LIMIT: 0.2220
        MAX THICKNESS: 2.0110
        MIN THICKNESS: 1.0110
        MEASURED VELOCITY: 0.2225
        MEASURED THICKNESS: 1.5215

我喜欢像这样将日期和速度线放在一行中: “2/20/2014 7:33:10 上午,测得的速度:0.2225”

这是我的问题

2/20/2014 7:33:10 AM,
,
,
,
,
,
,
,
,
,
,
,
,   MEASURED VELOCITY: 0.2225
,
2/20/2014 7:52:28 AM,
,
,
,
,
,
,
,
,
,
,
,
,   MEASURED VELOCITY: 0.2224
,
2/20/2014 7:58:46 AM,

   using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

namespace conApp
{
    class Program
    {

        static void Main(string[] args)
        {
            String line;
            try
            {
                using (StreamWriter sw = new StreamWriter("C:\writetest\writetest.txt"))
                {
                    string mydirpath = "C:\chat\";

                    string[] txtFileList = Directory.GetFiles(mydirpath, "*.txt");

                    foreach (string txtName in txtFileList)
                    {
                        System.IO.StreamReader sr = new System.IO.StreamReader(txtName);

                            while ((line = sr.ReadLine()) != null)
                            {
                                if (!string.IsNullOrEmpty(line))
                                {
                                    String spart = ".prt";
                                    String sam = " AM";
                                    String spm = " PM";
                                    String sresult = "TEST RESULT: ";
                                    String svelocity = "MEASURED VELOCITY: ";
                                    String part = "";
                                    String date = "";
                                    String result = "";
                                    String velocity = "";
                                    // sw.WriteLine(line);

                                    if (line.Contains(sam))
                                    {
                                        date = line;
                                    }

                                    if (line.Contains(spm))
                                    {
                                        date = line;
                                    }

                                    if (line.Contains(spart))
                                    {
                                        part = line;
                                    }

                                    if (line.Contains(sresult))
                                    {
                                        result = line;
                                    }

                                    if (line.Contains(svelocity))
                                    {
                                        velocity = line;
                                    }
                                    int I = 2;
                                    string[] x = new string[I];
                                    x[0] = date;
                                    x[1] = velocity;

                                    sw.WriteLine(x[0] + "," + x[1]);
                                }


                            }

                    }
                }
            }
            catch
            {

            }
        }
    }
}

只有当你有两个值时才写下这行: 然后将两个值都重置为 null。

sw.WriteLine(x[0] + "," + x[1]);

变成:

if ( !String.IsNullOrWhitespace( date) && !String.IsNullOrWhitespace( velocity )
{
  sw.WriteLine(x[0] + "," + x[1]);
  date = null;
  velocity = null;
}

正如 Blas 提到的,您还需要将变量移到 while 语句之外:

String result = "";
String velocity = "";
while ((line = sr.ReadLine()) != null)

您可以使用此正则表达式模式来实现您的目标:

(^.*?(?:AM|PM).*?)\r?\n.*(MEASURED VELOCITY:.*?$).*

代码如下:

using (StreamWriter sw = new StreamWriter("C:\writetest\writetest.txt"))
{
    string mydirpath = "C:\chat\";

    string[] txtFileList = Directory.GetFiles(mydirpath, "*.txt");

    Regex regex = new Regex("(^.*?(?:AM|PM).*?)\r?\n.*(MEASURED VELOCITY:.*?$).*",
        RegexOptions.Multiline | RegexOptions.Singleline);
    foreach (string txtName in txtFileList)
    {
        using (System.IO.StreamReader sr = new System.IO.StreamReader(txtName))
        {
            string text = sr.ReadToEnd();
            sw.WriteLine(regex.Replace(text, ", "));
        }
    }
}

给定文件示例的输出:

2/20/2014 7:33:10 AM, MEASURED VELOCITY: 0.2225

这是我对完整的 Main() 的建议,尝试尽可能多地使用您的代码。在 while 语句之外声明您的变量,您不需要将其设为 null。

编辑- 我忘了你说过:

Each read file has several of these items

所以添加了几行来处理这个问题。

static void Main(string[] args)
{
    string line;
    try
    {
        using (StreamWriter sw = new StreamWriter("C:\writetest\writetest.txt"))
        {
            string mydirpath = "C:\chat\";

            string[] txtFileList = Directory.GetFiles(mydirpath, "*.txt");

            foreach (string txtName in txtFileList)
            {
                string spart = ".prt";
                string sam = " AM";
                string spm = " PM";
                string sresult = "TEST RESULT: ";
                string svelocity = "MEASURED VELOCITY: ";
                string part = string.Empty;
                string date = string.Empty;
                string result = string.Empty;
                string velocity = string.Empty;

                using (StreamReader sr = new StreamReader(txtName))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (!string.IsNullOrEmpty(line) && line.Trim().Length != 0)
                        {
                            if (line.Contains(sam) || line.Contains(spm))
                            {
                                // Every new date means a new record. If you already have data for a record, first write it.
                                if (date != string.Empty && velocity != string.Empty)
                                {
                                    int I = 2;
                                    string[] x = new string[I];
                                    x[0] = date;
                                    x[1] = velocity;
                                    sw.WriteLine(x[0] + "," + x[1]);
                                }
                                // Then reset data to prepare it for a new record
                                part = string.Empty;
                                result = string.Empty;
                                velocity = string.Empty;
                                date = line;
                            }

                            if (line.Contains(spart))
                            {
                                part = line;
                            }

                            if (line.Contains(sresult))
                            {
                                result = line;
                            }

                            if (line.Contains(svelocity))
                            {
                                velocity = line;
                            }
                        }
                    }
                }

                // After last record you still have some data to write
                if (date != string.Empty && velocity != string.Empty)
                {
                    int I = 2;
                    string[] x = new string[I];
                    x[0] = date;
                    x[1] = velocity;
                    sw.WriteLine(x[0] + "," + x[1]);
                }
            }
        }
    }
    catch
    {

    }
}