How can i delete last column from my text file

强颜欢笑 提交于 2019-12-11 16:41:40

问题


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

Input text file looks like:

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"

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

Code snippet:

  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());
}

回答1:


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());
}



回答2:


var query = File.ReadLines(filename)
                .Where(line => !search_text.Any(text => line.Contains(text))) // <-- As requested by CodeCaster 
                .Select(line => String.Join("\t", line.Split('\t').Take(5)));
File.WriteAllLines(newfilename, query);

EDIT

Func<string, string> trim = s => s.Trim('“','”', '"', ' ' );

var query = File.ReadLines(filename)
            .Select(line => line.Split('\t'))
            .Where(parts => !parts.Any(part => search_text.Contains(trim(part))))
            .Where(parts => !parts.Any(part => trim(part) == "0"))
            .Select(parts => String.Join("\t", parts.Take(5)));



回答3:


This will do the job:

    string[] lines = File.ReadAllLines(@"c:\test.txt");
    string[] newLines = lines.Select(l =>
    {
        if (!string.IsNullOrEmpty(l)) // keep the empty rows
        {
            return l.Remove(l.LastIndexOf(' ')).TrimEnd();
        }

        return l;
    }).ToArray();
    File.WriteAllLines(@"c:\newtest.txt", newLines);



回答4:


A line contains 6 columns, so old.Split('\t') returns an array of 6 strings (if the input formatting is consistent).

In your loop you can select the first 5 elements and glue the array back together:

string new = string.Join("\t", old.Split('\t').Take(5));


来源:https://stackoverflow.com/questions/26940261/how-can-i-delete-last-column-from-my-text-file

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