在python中解析文本文件并创建键值对的字典,其中值以列表格式显示

如何按以下方式处理给定数据并使用dictionary存储它。

输入:

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(唯一),而与subject对应的值为通行证列表学生姓名

我的tcl工作代码如下,我想使用相同的逻辑将其转换为python。

parsing text file in tcl and creating dictionary of key value pair where values are in list format

            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]

我在使用python RE处理行数据时陷入困境。确实我无法将以下代码从tcl转换为python

            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
            }
            }