简体   繁体   中英

File line to dictionary of tuples

So, I want to write a file line to a dictionary of tuples.

Each line of a file is set up so a name matches a group of data. name,a,b,c,d each could be a different length in each set of data.

name: a: b: c: d

I want to create a dictionary that is set up like the following..

dict = {name : (a,b,c,d)}

how would i do that?

The first step is to split the line properly:

line = 'name: a: b: c: d\n'
pieces = line.split(':')
name = pieces[0]
tup = tuple(x.strip() for x in pieces[1:])

From here, it's just a matter of putting this in a loop and adding the name , tup to a dictionary which I trust you can do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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