简体   繁体   中英

read .txt file into a manipulatable string to convert into dictionary

I have a.txt file with these contents:

ACC210:

Luther, Martin

Spurgeon, Charles

CS121P:

Bunyan, John

Henry, Matthew

Luther, Martin

CS132S:

Calvin, John

Knox, John

Owen, John

I need to create a dictionary so it will look like this

{'ACC210': ['Luther, Martin', 'Spurgeon, Charles'] 'CS121P': ['Bunyan, John' 'Henry, Matthew' 'Luther, Martin'] 'CS132S': ['Calvin, John' 'Knox, John' 'Owen, John']}

How would I do that?

This is the method of creating a dictionary...

# Creating an empty dictionary
myDict = {}
  
# Adding list as value
myDict["key1"] = [1, 2]
myDict["key2"] = ["Geeks", "For", "Geeks"] 

So, for your case,

MyDict={}

MyDict["ACC210:"]=["Luther, Martin", "Spurgeon, Charles"]
MyDict["CS121P:"]= ... (continue)

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