Unexpected “,” from StreamReader to StreamWriter

戏子无情 提交于 2019-12-13 10:34:58

问题


I've tried a lot of things as well as research and asking friends, but can't seem to write a second line without a "," replacing the line. All I'd to do is have a single line for each item read in a separate file.

Each read file has several of these items:

2/20/2014 7:33:10 AM
OPERATOR: jason
FILE: C:\ax14\Setups\000062363106RH.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

Id like to have the date and velocity line in one line like this: "2/20/2014 7:33:10 AM, MEASURED VELOCITY: 0.2225"

and this is my problem

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
            {

            }
        }
    }
}

回答1:


Here is my suggestion for the full Main() trying to use as much from your code as possible. Declaring your vars outside the while statement you don't need to make it null.

EDIT- I forgot you said:

Each read file has several of these items

So added a few lines to handle that.

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
    {

    }
}



回答2:


Only write the line once you have both values: Then reset both values to null.

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

Becomes:

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

As Blas mentioned you also need to move the variables outside the while statement:

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



回答3:


You can use this Regex Pattern to Achieve your goal :

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

And here's the code:

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, "$1, $2"));
        }
    }
}

Output for given file example:

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



来源:https://stackoverflow.com/questions/30646578/unexpected-from-streamreader-to-streamwriter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!