简体   繁体   English

从文本文件中提取数据

[英]Extract data from text file

I need to extract some data from a text file and insert to columns in excel sheet. 我需要从文本文件中提取一些数据,然后插入到Excel工作表中的列中。 I know how to do this if the rows and the length of the string is known. 我知道如果知道行和字符串的长度该怎么做。

try
{
    using (System.IO.StreamReader sr = new System.IO.StreamReader("test.txt")
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            listSNR.Items.Add(line.Substring (78,4));
        }
    }
}

But the particular text file is complex and the starting index or the length cannot be provided. 但是特定的文本文件很复杂,无法提供起始索引或长度。 But the starting word (PCPU01) of the row is known. 但是该行的起始字(PCPU01)是已知的。 Eg: PCPU01,T2716,0.00,0.01,0.00,0.00 例如: PCPU01,T2716,0.00,0.01,0.00,0.00

output: T2716 0 0.01 0 0 输出: T2716 0 0.01 0 0

In that case can somebody please let me know how to extract the texts? 在这种情况下,可以让我知道如何提取文本吗?

using(System.IO.StreamReader sr = new System.IO.StreamReader("test.txt"))
{
    string line;
    while((line = sr.ReadLine()) != null)
    {
        string[] split = line.Split(',');
        //...
    }
}

split[0] will return "PCPU01" , split[1] "T2716" and so on. split[0]将返回"PCPU01"split[1] "T2716" ,依此类推。

You can split one string into an array of strings, separated by a given character. 您可以将一个字符串拆分为由给定字符分隔的字符串数组。 This way, you could split the source string by a comma and use the resulting strings to build your output. 这样,您可以用逗号分隔源字符串,然后使用生成的字符串构建输出。 Example: 例:

string source = "PCPU01,T2716,0.00,0.01,0.00,0.00";
string[] parts = source.Split(',');
StringBuilder result = new StringBuilder();

result.Append(parts[1]); // The second element in the array, i.e. T2716
result.Append(" ");
result.Append(parts[2]); // 0.00
... // And so on...
return result.ToString() // return a string, not a StringBuilder

I hope this helps a little bit. 我希望这会有所帮助。 You might have to tweak it to your needs. 您可能需要根据需要进行调整。 But this is a higher level code that gives you general idea of extracting data off a notepad. 但这是一个更高级别的代码,可让您大致了解如何从记事本中提取数据。

DialogResult result = openFileDialog.ShowDialog();

            Collection<Info> _infoCollection = new Collection<Info>();
            Collection<string> listOfSubDomains = new Collection<string>();

            string[] row;
            string line;

            // READ THE FILE AND STORE IT IN INFO OBJECT AND STORE TAHT INFO OBJECT IN COLLECTION
            try
            {
                using (StreamReader reader = new StreamReader(openFileDialog.FileName))
                {
                    while((line = reader.ReadLine()) != null)
                    {
                        Info _info = new Info();
                        row = line.Split(' ');

                        _info.FirstName = row[0];
                        _info.LastName = row[1];
                        _info.Email = row[2];
                        _info.Id = Convert.ToInt32(row[3]);

                        _infoCollection.Add(_info);

                    }
                }

catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

thanks for the answers. 感谢您的回答。 What i wanted is to identify the particular line in the text file and split the line into columns. 我想要的是识别文本文件中的特定行,并将该行拆分为列。 So i was able to do this by calling a GetLine method: 所以我能够通过调用GetLine方法来做到这一点:

string line15=GetLine(@"test.txt",15);
public string GetLine(string fileName, int line)
    {
        using (System.IO.StreamReader ssr = new System.IO.StreamReader("test.txt")) 
       //using (var ssr = new StreamReader("test.txt"))
        {
            for (int i = 1; i < line; i++)
                ssr.ReadLine();
            return ssr.ReadLine();
        }
    }

Then i splitted this line by using the delimiter (,) 然后我使用定界符(,)分割了这一行

This was my approach in C#. 这是我在C#中使用的方法。 It takes a string input (which you can get out of a text file) and an int with which line you want to get. 它需要一个字符串输入(可以从文本文件中取出)和一个要获取哪一行的整数。 It then separates the string at a given seperator char to a list which in turn is then read out. 然后,它将在给定分隔符char处的字符串分隔为一个列表,然后依次读取该列表。 If the given line number is lower than the count of the created list, the entry is given back. 如果给定的行号小于创建的列表的计数,则将回送该条目。

    public string GetLine(string multiline,int line)
    {
        List<string> lines = new List<string>();
        lines = multiline.Split('\n').ToList<string>();

        return lines.Count >= line ? lines[line] : "";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM