Dictionary 解析tcl中的文本文件并创建键值对字典,其中值为列表格式

Dictionary 解析tcl中的文本文件并创建键值对字典,其中值为列表格式,dictionary,tcl,text-parsing,Dictionary,Tcl,Text Parsing,如何分离以下文本文件并保留相应的仅需要数据: 例如,文本文件具有以下格式: Name Roll_number Subject Experiment_name Marks Result Joy 23 Science Exp related to magnet 45 pass Adi 12 Science Exp electronics 48 pass kumar 18 Maths prime numbers 4

如何分离以下文本文件并保留相应的仅需要数据:

例如,文本文件具有以下格式:

Name Roll_number Subject Experiment_name Marks Result
Joy  23          Science Exp related to magnet 45 pass
Adi  12          Science Exp electronics       48 pass
kumar 18         Maths   prime numbers         49 pass
Piya 19          Maths   number roots          47 pass
Ron 28           Maths   decimal numbers       12 fail
解析上述信息并存储在字典中后,其中键为subject(unique),与subject对应的值为pass Student name列表

set studentInfo [dict create]; # Creating empty dictionary
set fp [open input.txt r]
set line_no 0
while {[gets $fp line]!=-1} {
    incr line_no
    # Skipping line number 1 alone, as it has the column headers
    # You can alter this logic, if you want to 
    if {$line_no==1} {
        continue
    }
    if {[regexp {(\S+)\s+\S+\s+(\S+).*\s(\S+)} $line match name subject result]} {
        if {$result eq "pass"} {
            # Appending the student's name with key value as 'subject'
            dict lappend studentInfo $subject $name
        }
    }
}
close $fp
puts [dict get $studentInfo]
输出:

Science {Joy Adi} Maths {kumar Piya}

不计算行号,您可以在打开文件后简单地
获取$fp header
。我们是否可以为所有上述信息创建结构并创建结构列表的dict,其中key是roll\u number?我们是否可以为所有上述信息创建结构并创建结构列表的dict,其中key是roll\u number??