【问题标题】:How to remove the all the double quotes and allign every thing in perfect format from a text file如何从文本文件中删除所有双引号并以完美格式对齐所有内容
【发布时间】:2014-12-25 17:50:31
【问题描述】:

我想从下面的文本文件中删除双引号。我需要使用我的 C# 应用程序输入文件并读取文本文件,然后删除所有双引号并给出输出。 我需要删除所有引号并将它们变成完美的四行距格式 那么我该怎么做呢。

输入文件

   Designator   Comment Footprint   Center-X(mm)    Center-Y(mm)

     "C5"   "CAP,1n,50V.0603"   "CAPC1608N" "97.733"    "73.025"
     "C4"   "CAP,1n,50V.0603"   "CAPC1608N" "99.638"    "73.025"
     "C3"   "CAP,1n,50V.0603"   "CAPC1608N" "101.543"   "73.025"
     "C7"   "CAP1233,,1n,50V.0603"  "CAPC1608N" "103.448"   "73.025"
     "C8"   "CAP1233,1n,50V.0603"   "CAPC1608N" "105.353"   "73.025"
     "C9"   "CAP455,1n,50V.0603"    "CAPC1608N" "107.258"   "73.025"
     "C10"  "CAP4522,1n,50V.0603"   "CAPC1608N" "109.163"   "73.025"

我的 C# 应用程序中用于输入文本文件的代码:

private void convert_Click(object sender, EventArgs e)
{
   OpenFileDialog ofd = new OpenFileDialog();
        ofd.InitialDirectory = @"C:\files";
        ofd.Filter = "TXT files (*.txt)|*.txt";
        ofd.FilterIndex = 2;
        ofd.RestoreDirectory = true;
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            int[] cols = new int[] { 15, 30, 15, 20, 12 };
            string[] strLines = System.IO.File.ReadAllLines(fileName);

            StringBuilder sb = new StringBuilder();
            string line = string.Empty;
            for (int i = 0; i < strLines.Length; i++)
            {
                line = RemoveWhiteSpace(strLines[i]).Trim(); // here i am getting error in RemoveWhiteSpace, that it is not in the context
                string[] cells = line.Replace("\"", "").Split(new string[] { " " }, StringSplitOptions.None);
                for (int c = 0; c < cells.Length; c++)
                    sb.Append(cells[c].PadRight(cols[c]));
                sb.Append("\r\n");
            }

            //System.IO.File.WriteAllText(fileName, sb.ToString());
            MessageBox.Show(sb.ToString());
        }
}

我该怎么做?请帮帮我。

我想为我输入的所有文本文件获取以下样式的格式。所以对于上面的文本文件,我想得到如下所示的格式

     Designator    Comment          Footprint   Center-X(mm)    Center-Y(mm)

       C5      CAP,1n,50V.0603      CAPC1608N     97.733          73.025
       C4      CAP,1n,50V.0603      CAPC1608N     99.638          73.025
       C3      CAP,1n,50V.0603      CAPC1608N     101.543         73.025
       C7      CAP,1n,50V.0603      CAPC1608N     103.448         73.025
       C8      CAP,1n,50V.0603      CAPC1608N     105.353         73.025
       C9      CAP,1n,50V.0603      CAPC1608N     107.258         73.025
       C10     CAP,1n,50V.0603      CAPC1608N     109.163         73.025

【问题讨论】:

    标签: c# winforms double-quotes file-conversion


    【解决方案1】:

    这两个步骤是读取文件并将引号替换为空。

    string allText = File.ReadAllText(ofd.FileName); //read file
    string noQuotes = allText.Replace("\"", ""); //replace text
    

    字符串是不可变的,这就是为什么Replace 返回一个新的字符串对象而不是简单地更改现有的allText 字符串。

    或者,您可能希望使用ReadAllLines 来代替,它将返回一个字符串数组,其中每个元素都是文本文件的一行。理论是一样的,但你会迭代数组并用空字符串替换每个元素中的引号。

    如果你想使用 ReadAllLines 和 WriteAllLines(保存文件),你可以这样做

    File.WriteAllLines(@"saveFile.txt", File.ReadAllLines(ofd.FileName)
                                       .Select(s => s.Replace("\"", "")).ToList());
    

    这将在一行中完成读取、替换和保存(并不是说行数越少越好)。

    【讨论】:

    • 对不起,应该是FileName,大写N(我已经在答案中更新了)
    • 您可以使用File.WriteAllTextFile.WriteAllLines。如果您使用的是ReadAllLines,则为后者。
    • 您尝试改用WriteAllLines 吗?它应该保持换行符。你的输出是什么样的?
    • 您能澄清一下您的意思吗?正在输出的文件(在上面的示例中称为“saveFile.txt”)是否与选择的原始文件相同?别忘了,上面的代码没有覆盖被选中的文件,而是创建了一个新文件(称为 saveFile.txt,但您可以将其更改为您喜欢的任何内容)
    • 看起来您的目标是 .NET 3.5。将ToList 更改为ToArray
    【解决方案2】:

    您可以使用正则表达式轻松做到这一点:

    using System.IO;
    using System.Text.RegularExpressions;
    
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        string withoutQuotes = Regex.Replace(File.ReadAllText(ofd.FileName), "\"", "");
        File.WriteAllText(ofd.FileName, withoutQuotes);
    }
    

    【讨论】:

    • 这行得通,但我的格式消失了..我怎样才能得到正确的格式
    【解决方案3】:

    我会这样做:

    using System.IO;
    
    File.WriteAllText(@"C:/text2.txt", File.ReadAllText(@"C:/text1.txt").Replace("\"", ""));
    

    更新:更短的解决方案

    【讨论】:

      【解决方案4】:

      试试这个例子,这将删除双引号并重新格式化您的文本。

      private void Reformat(string fileName)
      {
          //Give the column width according to column index.
          int[] cols = new int[] { 12, 35, 25, 15, 15 };
          string[] strLines = System.IO.File.ReadAllLines(fileName);
      
          StringBuilder sb = new StringBuilder();
          string line = string.Empty;
          string LastComment = string.Empty;
          string CarouselName = "Carousel";
          int iCarousel = 0;
      
          for (int i = 0; i < strLines.Length; i++)
          {
              line = RemoveWhiteSpace(strLines[i]).Trim();
              string[] cells = line.Replace("\"", "").Split('\t');                    
              for (int c = 0; c < cells.Length; c++)
                  sb.Append(cells[c].Replace(" ", "_").PadRight(cols[c]));
      
              if (cells.Length > 1)
              {
                  if (cells[1] != LastComment & i > 0)
                  {
                      iCarousel++;
                      if (iCarousel > 45)
                         iCarousel = 1;
                      LastComment = cells[1];
                  }
      
                  if (i == 0)
                      sb.Append("Location".PadRight(15));
                  else
                      sb.Append(String.Format("{0}:{1}", CarouselName, iCarousel).PadRight(15));
              }
              sb.Append("\r\n");
          }
      
          //System.IO.File.WriteAllText(fileName, sb.ToString());
          MessageBox.Show(sb.ToString());
      }
      
      
      private string RemoveWhiteSpace(string str)
      {
          str = str.Replace("  ", " ");
          if (str.Contains("  "))
              str = RemoveWhiteSpace(str);
          return str;
      }
      

      【讨论】:

      • 这里我在 RemoveWhiteSpace 中遇到错误,它不在上下文中
      • 你在那里.. 你能给我一个在 stackover flow 中聊天的请求吗
      猜你喜欢
      • 2023-01-11
      • 2017-04-21
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2021-04-20
      相关资源
      最近更新 更多