【问题标题】:Unexpected "," from StreamReader to StreamWriter意外的“,”从 StreamReader 到 StreamWriter
【发布时间】:2015-06-04 14:26:52
【问题描述】:

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

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

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

我喜欢将日期和速度线放在一行中,如下所示: “2014 年 2 月 20 日上午 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
            {

            }
        }
    }
}

【问题讨论】:

    标签: c# logic


    【解决方案1】:

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

    编辑-我忘了你说过:

    每个读取的文件都有几个这样的项目

    所以添加了几行来处理它。

    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
        {
    
        }
    }
    

    【讨论】:

    • 甜蜜!我没有 4.0 VS 框架能够使用 IsNullOrWhiteSpace 方法,
    • @TheodoreCross 将 IsNullOrWhiteSpace 替换为自 .NET 2.0 起有效的等效代码
    【解决方案2】:

    只有当你拥有这两个值时才写这行: 然后将这两个值都重置为 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)
    

    【讨论】:

    • 问题是你每次通过循环时都在写一行,例如您正在为原始文件中的每一行写一行。
    • 注意:已编辑 - 重置日期和速度应该发生在 if 语句中。
    • 可以在 if 语句中重置日期和速度,但也需要在 while 语句之外初始化这 2 个变量。
    • 他还应该通过使用using 语句或调用Dispose() 来处理StreamReader
    【解决方案3】:

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

    (^.*?(?: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, "$1, $2"));
            }
        }
    }
    

    给定文件示例的输出:

    2/20/2014 上午 7:33:10,测得的速度:0.2225

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 2014-05-22
      • 2019-02-26
      • 2016-08-23
      • 2019-06-11
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 2021-12-25
      相关资源
      最近更新 更多