如何从我的文本文件中删除最后一列 [英] How can i delete last column from my text file

查看:37
本文介绍了如何从我的文本文件中删除最后一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的文本文件中删除最后一列.我怎样才能做到这一点.我的文本文件以制表符分隔.

I would like to delete the last column from my text file. How can i achieve this. My text file is TAB delimited.

输入文本文件如下:

Designator     MAX PN        Footprint         Center-X(mm)   Center-Y(mm)   Location

 "C10"  "100-0009"  "1206 - CAPACITOR"  "122.492"   "69.469"     "BOTTOM"
 "DOC1" "100-0009"  "1206 - CAPACITOR"  "264.211"   "12.814"     "BOTTOM"
 "C102" "100-0009"  "1206 - CAPACITOR"  "251.346"   "11.201"     "BOTTOM"
 "C105" "100-0009"  "1206 - CAPACITOR"     "0"         "0"     "BOTTOM"
 "PCB"  "100-0009"  "1206 - CAPACITOR"  "306.197"   "29.909"      "BOTTOM"
 "C107" "100-0009"  "1206 - CAPACITOR"  "273.685"   "29.527"     "BOTTOM"

我想从文本文件中删除整个名为 Location 的列..并处理输出..

I Want to delete the entire column called Location from the text file.. and process the output..

代码片段:

  public void do_name()
{
    string[] search_text = new string[] { "PCB", "DOC", "PCB1", "DOC1" };
    string old;
    StringBuilder sb = new StringBuilder();
    using (StreamReader sr = File.OpenText(textBox1.Text))
    {
        while ((old = sr.ReadLine()) != null)
        {
            if (old.Contains(search_text[0]) || old.Contains(search_text[1]) ||
                old.Contains(search_text[2]) || old.Contains(search_text[3]) ||
               old.Split('\t').Contains(@"""0"""))
                continue;
            else
                sb.AppendLine(old);
        }
        sr.Close();
    }
    File.WriteAllText(textBox1.Text, sb.ToString());
}

推荐答案

处理前只需删除行中的最后一列.

Just remove the last column from the line before processing.

public void do_name()
{
    bool headerRead = false;
    bool layerColumnPresent = false;
    string[] search_text = new string[] { "PCB", "DOC", "PCB1", "DOC1" };
    string old;
    StringBuilder sb = new StringBuilder();
    using (StreamReader sr = File.OpenText(textBox1.Text))
    {
        while ((old = sr.ReadLine()) != null)
        {
            if (!headerRead)
            {
                layerColumnPresent = old.Substring(old.LastIndexOf("\t").ToLower()).Contains("layer")
                headerRead = true;
            }
            if (old.Length > 0 && layColumnPresent) // if not a zero length string
            {
                old = old.Substring(0, old.LastIndexOf("\t"));
            }
            if (old.Contains(search_text[0]) || old.Contains(search_text[1]) ||
                old.Contains(search_text[2]) || old.Contains(search_text[3]) ||
                old.Split('\t').Contains(@"""0"""))
                continue;
            else
                sb.AppendLine(old);
        }
        sr.Close();
   }
   File.WriteAllText(textBox1.Text, sb.ToString());
}

这篇关于如何从我的文本文件中删除最后一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆