簡體   English   中英

我正在嘗試解析一個沒有太多一致性的文件並獲取用戶ID,名字和姓氏

[英]I am attempting to parse a file which does not have much consistency and grab the user id's, first and last name

我正在嘗試分析一個沒有太多一致性的文件,並抓住用戶ID的名字和姓氏。

一些用戶ID沒有名稱,並顯示為“找不到”。 有些每行有不同數量的項目,因此有多個if / else if /塊。

我有一個用戶ID字典作為鍵和一個struct對象,其中包含名字和姓氏作為值。 這是我大部分的代碼,我想向每個字典鍵(用戶ID)添加一個對象(名字和姓氏),但是目前它正在向每個鍵(用戶)添加整個文件,包括名字和姓氏ID)。

我在while循環中有dictionary.add,所以我認為它將正確地將它們添加到字典中。 我究竟做錯了什么?

如果我不正確地打印出密鑰和列表值,請告訴我。

編輯:修復了由於“ SACH”而導致的多次打印問題。 我現在的另一個問題是解析。 該文件本身具有敏感信息,因此我將盡力提供類似於示例的內容。

l User_ID SECTION(header)                                                                                                                               
  USER_ID                 text     USER_ID     L_Name F_Name some_digits                                                                                  
                          another line or two of unimportant info



l USER_ID SECTION(header)                                                                                                                               
  USER_ID                 text  letter  USER_ID   L_Name M_Name F_Name                                                                                        
                          more lines of unimportant info                       

我的問題是,這不是文件中所有條目的標准。 而且,如果我不確定該期待什么文字,該如何持續獲得名稱

我獲取ID沒問題,但條目不一致,我抓到的一些名稱不正確。 有沒有辦法在不知道名稱或字符串的情況下獲取名稱?

    public static void read_file() {

        //variable declaration
        string user_id = "*"; 
        string file_path = @"c:\users\blah\blah";
        string line_of_text;

        List<full_name> Name = new List<full_name>();

        //define dictionary for user id, last name, first name
        Dictionary<string, List<full_name>> dict = new Dictionary<string, List<full_name>>();

        var filestream = new System.IO.FileStream(file_path,
                                 System.IO.FileMode.Open,
                                 System.IO.FileAccess.Read,
                                 System.IO.FileShare.ReadWrite);
        var file = new System.IO.StreamReader(filestream, System.Text.Encoding.UTF8, true, 128);


        //loop through reading text file
        while ((line_of_text = file.ReadLine()) != null)
        {

            string[] temp = line_of_text.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);


            //check if first line (begins with 'l')
            //if first line, add first and last name to list Name
            int pos = Array.IndexOf(temp, "l");
            if (pos > -1)
            {
                user_id = temp[1];
                line_of_text = file.ReadLine();
                string[] line2 = line_of_text.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                int pos2 = Array.IndexOf(line2, "NOT");
                if (pos2 > -1) {
                    Name.Add(new full_name()
                    {
                        f_name = "NOT",
                        l_name = "FOUND"
                    });
                }

                else if (line2.Length == 4)
                {
                    Console.WriteLine("F: {0}, L: {1}", line2[3], line2[2]);
                    Name.Add(new full_name()
                    {
                        f_name = line2[3],
                        l_name = line2[2]
                    });
                }
                else if (line2.Length == 6)
                {
                    Console.WriteLine("F: {0}, L: {1}", line2[5], line2[4]);
                    Name.Add(new full_name()
                    {
                        f_name = line2[5],
                        l_name = line2[4]
                    });
                }
                else {
                    Console.WriteLine("F: {0}, L: {1}", line2[4], line2[3]);
                    Name.Add(new full_name()
                    {
                        f_name = line2[4],
                        l_name = line2[3]
                    });
                }
            }




            if (!dict.ContainsKey(user_id))
            {
                dict.Add(user_id, Name);
            }
        }
        int k = 1;
        int m = 1;
        foreach (KeyValuePair<string, List<full_name>> kvp in dict){
            foreach( full_name entry in Name){
                Console.WriteLine("{0}  ID: {1}  Last Name: {2}  First Name: {3}", k, kvp.Key, entry.l_name, entry.f_name);
                k++;
            }
        }
    }

這是我的結構:

struct full_name {
    public string f_name;
    public string l_name;
}

List<full_name> Name = new List<full_name>(); 在函數級別聲明。 在循環中聲明它。 它不斷追加到功能級別列表,並將其添加到每個用戶ID。

List<full_name> Name是在讀取文件的循環之外定義和實例化的。

然后在循環中,每當您讀一行時,便向Name添加一條記錄,並在循環結束之前將Name添加至dict ,以便隨着循環的進行,您的Name列表會增加。

您需要做的是每次在循環內創建一個Name實例(而不是列表),然后將其添加到字典中。

while ((line_of_text = file.ReadLine()) != null)
{
    // Stuff
    full_name name = new full_name();
    if (pos > -1)
    {
        ...
        name.f_name = "NOT";
        name.l_name = "FOUND";
    }

    // Other if statements

    if (!dict.ContainsKey(user_id))
    {
        dict.Add(user_id, name);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM