简体   繁体   English

如何使用 ArrayList 解析 c# 中的结构化文件?

[英]How to parse a structured file in c# with ArrayList?

I have a text file like this.我有一个这样的文本文件。 在此处输入图像描述

I convert it into a structured format like below.我将其转换为如下所示的结构化格式。

public static ArrayList ConllToStructured(string filepath)
    {
        ArrayList structuredfile = new ArrayList();
        string currentline;
        int currentsentence = 0;
        //Read conll file and Convert it to a structured Array list
        //First column is sentence number and other 12 columns is conll fileds

        using (StreamReader sr = new StreamReader(filepath, Encoding.UTF8))
        {
            while ((currentline = sr.ReadLine()) != "#end document")
            {
                if (currentline.Contains("#begin document")) continue;
                if (string.IsNullOrEmpty(currentline))
                {
                    currentsentence += 1;
                    continue;
                }

                //parse each line with whitespace and save in array
                var words = currentline.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                //Save sentence number and word array into a temp array list
                List<string> templine = new List<string>();
                templine.Add(currentsentence.ToString());
                foreach (var columns in words)
                {
                    templine.Add(columns);
                }


                structuredfile.Add(templine);
            }
        }
        return structuredfile;
    }

But, when I want to access this structured file, I can only access to each row but I can't access each column in rows.但是,当我想访问这个结构化文件时,我只能访问每一行,但不能访问行中的每一列。 I add arrays of string to ArrayList for further access.我将字符串的 arrays 添加到 ArrayList 以供进一步访问。

From what I understood what you are asking in your question is how from the returned ArrayList you can get given column for given row.据我了解,您在问题中所问的是如何从返回的ArrayList中获得给定行的给定列。

I think it would be a better option to return from this method List<List<string>> .我认为从这个方法返回List<List<string>>会是一个更好的选择。 Then later when you want to access specific column in a specific row.然后稍后当您想要访问特定行中的特定列时。 You can just do你可以做

var structuredFile = ConllToStructured(filepath);
var columnString = structuredFile[rowIndex][colIndex];

But if for some reason you need it to be ArrayList you should know that ArrayList is always from objects.但是如果由于某种原因你需要它是ArrayList你应该知道ArrayList总是来自对象。 So when you access an element from it you will be returned object, which you need to cast to List<string> so you would do something like this:因此,当您从中访问元素时,您将返回 object,您需要将其转换为List<string> ,因此您可以执行以下操作:

var columnString = (structuredFile[rowIndex] as List<string>)[colIndex];

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

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