Team members 11
Team members 12
Team members 13
+ group 2
Group 21
Team members 22
Team members 23
3 + leader
Group 31
Team members 32
Team members 33
TXT text above, I'd like to read out the final deposit into a dictionary
{group leader 1: [13] group team members, 11 to 12, and team members, team leader 2: [23] group members, 21 to 22, student, group 3: [members] 31}
Readlines out each row data, deposited in the list
Using if judgment is whether the element has a '+'
Do the dictionary by key, do without the '+' values,
But don't know how to write, the this loop, and a bit of a mess,, what a great god help le le, do not appreciate,
CodePudding user response:
First, create an empty dictionary dic={}Began to traverse each row +, if so, remove the + and line breaks, extracted elements as a new key values of the dictionary, the key value of the value initialized to an empty list,
If there is no +, that is extracted in a key value corresponding to the list of an element, remove the newline after append,
Filename='get - group. TXT'
File_con=[]
With the open (filename, 'r') as pf:
File_con=pf. Readlines ()
Print (file_con)
Dic={}
For the line in file_con:
If the '+' in the line:
Key=line. Strip (' + '). The strip (' \ n ')
Dic [key]=[]
The else:
Value=https://bbs.csdn.net/topics/line.strip (' \ n ')
[key]. Dic append (value)
Print (dic)